vue component for listing a blog in typescript

Here is an example Vue component for listing a blog using Typescript:

index.ts
<template>
  <div>
    <h2>{{ title }}</h2>
    <ul>
      <li v-for="post in posts" :key="post.id">
        <router-link :to="{ name: 'blogPost', params: { id: post.id } }">
          {{ post.title }}
        </router-link>
      </li>
    </ul>
  </div>
</template>

<script lang="ts">
import Vue from 'vue';
import { Component } from 'vue-property-decorator';
import { BlogPost } from '@/models/blog-post'; // assuming you have a BlogPost interface or class

@Component({})
export default class BlogList extends Vue {
  title = 'Latest Posts';
  posts: BlogPost[] = [];

  created() {
    this.getPosts();
  }

  async getPosts() {
    // assuming you have a Vuex store and action to fetch the blog posts
    await this.$store.dispatch('fetchBlogPosts');

    this.posts = this.$store.getters.blogPosts;
  }
}
</script>
823 chars
36 lines

In this component, we have a title that will be displayed above the list of blog posts. We fetch the blog posts in the created lifecycle hook using a Vuex action called fetchBlogPosts, and then map them to the posts array. We then use v-for to loop through the posts array and display each post's title as a link to the BlogPost component (which assumes there is another component for displaying an individual blog post).

gistlibby LogSnag