Vue 创建博客前端-DAY3-文章详情页面

2020-03-05  本文已影响0人  ricekent
添加文章详情页面

view 目录下新建 ArticleView.vue 展示文章详情。

配置路由

router/index.ts 中,将 ArticleView 添加为 Main 的子路由,并在 meta 中设置 showList=false

import ArticleView from "../views/ArticleView.vue";

const routes = [
   ...
    children: [
      {
        path: "/article/:id",
        name: "article-detail",
        component: ArticleView,
        props: true,
        meta: {
          showList: false
        }
      }
    ]
];
读取文章信息

ArticleView.vue<script></script>标签中添加:

import { Vue, Component, Prop } from "vue-property-decorator";

@Component({})
export default class PostDetail extends Vue {
  @Prop(String) id!: string;

  article = {};

  async fetch() {
    const res = await this.$http.get(`posts/${this.id}`, {});
    this.article = res.data;
  }

  created() {
    this.fetch();
  }
}
上一篇 下一篇

猜你喜欢

热点阅读