【问题标题】:How to update the meta information from dynamic data using vue-meta in Vue js?如何在 Vue js 中使用 vue-meta 从动态数据中更新元信息?
【发布时间】:2020-03-26 22:20:31
【问题描述】:

我的文章有各自的元描述。我正在使用vue-meta 替换默认的元描述。我尝试使用 async 和 mount 属性从我的 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>

我将不胜感激,谢谢!

【问题讨论】:

  • 您在使用 Nuxt 吗?另外,您能否在浏览器的网络选项卡中查看是否正在获取数据?
  • @DelenaMalan 是的,我正在使用 Nuxt,对我的 API 的调用确实返回了一些东西。

标签: javascript vue.js vuejs2 nuxt.js vue-meta


【解决方案1】:

您好,您可以使用serverPrefetch,而不是使用asyncData

试试这个

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

  }

【讨论】:

  • 您好,感谢您的回答。你能扩大你的答案吗?我对该方法中返回的内容以及如何使用它感到有些困惑。
  • 嗨@AlyssaAlex 我已经更新了代码,实际上serverPrefetch 应该返回一个Promise。
  • 您好,很遗憾,在从我的网络选项卡检查此方法时,我的 API 没有任何响应。我应该在挂载方法中保留this.articlelist = this.serverPrefetch(); 吗?
猜你喜欢
  • 1970-01-01
  • 2018-06-28
  • 2020-01-24
  • 2017-04-11
  • 1970-01-01
  • 1970-01-01
  • 2018-07-08
  • 1970-01-01
  • 2018-04-01
相关资源
最近更新 更多