Javascript 如何在Vue js中为头部对象使用异步函数?
我必须为我的文章显示动态的元描述,而我正努力使用head对象的异步函数来实现这一点。这就是我到目前为止所做的: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
<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
}