0

我的文章有各自的元描述。我vue-meta用来替换默认的元描述。我尝试使用 async 和mounted 属性从我的API 中获取信息,但我没有看到头部有任何变化,即在相应文章的元描述中。我仍然看到 Vue js 设置的默认值。

这就是我所拥有的:

<script lang="ts">

import { Vue } from 'vue-property-decorator';
import VueMeta from 'vue-meta';

Vue.use(VueMeta);

export default class ArticleContent extends Vue {
  article: any | null = null;
  articlelist: any = null;
  id = 1;

  async mounted(): Promise<any> {
    this.article = this.articlelist.find((f: any) => {     <-- slug
      return f.title_slug === this.$route.params.id;
    });
    this.articlelist = await this.asyncData();
  }

  async asyncData(): Promise<any> {
    const articlelist = await this.$axios.get(             <-- call to my api
      'https://my_api...'
    );
    return articlelist.data.data;
  }

    metaInfo(): any {                                      <-- meta information
    return {
      title: 'Article',
      meta: [
        {
          hid: this.articlelist[0]._id,
          name: this.articlelist[0].productNames['en'],
          content: this.articlelist[0].metaDescription['en'],
        },
      ],
    };
  }
}
</script>

我会很感激一些帮助,谢谢!

4

1 回答 1

0

嗨,而不是使用asyncData你可以使用serverPrefetch

尝试这个

 serverPrefetch():Promise<any> {
  // this function should return Promise
   return this.$axios.get(
      'https://my_api...'
    ).then(result => {
          this.articlelist = result.data.data
      });

  }
于 2019-12-02T12:22:22.867 回答