Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Vue js中为头部对象使用异步函数?_Javascript_Typescript_Vue.js_Nuxt.js - Fatal编程技术网

Javascript 如何在Vue js中为头部对象使用异步函数?

Javascript 如何在Vue js中为头部对象使用异步函数?,javascript,typescript,vue.js,nuxt.js,Javascript,Typescript,Vue.js,Nuxt.js,我必须为我的文章显示动态的元描述,而我正努力使用head对象的异步函数来实现这一点。这就是我到目前为止所做的: <script lang="ts"> import { Component, Vue } from 'vue-property-decorator'; @Component export default class ArticleContent extends Vue { article: any | null = null; articlelist = exa

我必须为我的文章显示动态的元描述,而我正努力使用head对象的异步函数来实现这一点。这就是我到目前为止所做的:

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';

@Component

export default class ArticleContent extends Vue {
  article: any | null = null;
  articlelist = example.data;

  async asyncData({params: any }) { <----- Not sure how I could put in my articlelist here
    return this.articlelist;
  }

  head(): object {
    return {
      title: this.articlelist.productId.productNames['en'],
      meta: [
        {
          hid: this.articlelist._id,
          name: this.articlelist.productNames['en'],
          content: this.articlelist.metaDescription['en'],
        },
      ],
    };
  }
}
</script>

从“Vue属性装饰器”导入{Component,Vue};
@组成部分
导出默认类ArticleContent扩展Vue{
第条:任何|空=空;
articlelist=example.data;
async asyncData({params:any}){属性
head()
asyncData()
都不是vue核心的一部分

  • 要使用
    head()
    您需要作为插件安装
  • 要使用
    asyncData()
    ,您必须使用
如果你的spa对seo有强烈的需求,我建议你使用nuxt,它本来就包括seo,从vue到nuxt的转换非常容易

如果您已经在使用nuxt,这是获得

async asyncData({params: any }) { 
 const articlelist = await axios.get('some.url'); //get the data
 return { articlelist }; //this object is merged with the data of the istance
}