首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对计算属性使用vue-meta

对计算属性使用vue-meta
EN

Stack Overflow用户
提问于 2018-11-21 23:05:06
回答 1查看 1.7K关注 0票数 0

我正试着给我的文章页面添加动态的meta标签。文章存储在VueX存储中,我使用计算属性来获取要显示的文章:

代码语言:javascript
复制
computed: {
  article() {
   return this.$store.state.articles.filter(article => article.id == this.$route.params.id);
  }
}

为此,我使用了vue-meta (有没有更好的方法?我没有使用Nuxt,所以我没有服务器端渲染)。

正确的使用方法应该是:

代码语言:javascript
复制
metaInfo() {
  return {
    title: this.article.title,

    meta: [
     {property: 'og:title', content: this.article.title},
     {property: 'og:site_name', content: 'Website Name'},
     {property: 'og:type', content: 'website'},
     {property: 'og:url', content: 'url.com'},
     {property: 'og:image', content: this.article.image},
     {property: 'og:description', content: this.article.description},
     ]
   }
 }

但只有当文章存储在data()中时,它才能起作用。由于组件返回动态项目,如何访问计算属性中经过筛选的项目?

感谢您的帮助

EN

回答 1

Stack Overflow用户

发布于 2018-11-21 23:08:57

您需要将计算属性命名为article,这也要归功于@ssc-hrep2使用find而不是filter返回数组中匹配的对象,因为filter返回一个数组:

代码语言:javascript
复制
computed: {
  article () {
    return this.$store.state.articles.find(article => article.id == this.$route.params.id)
  }
}

或者使用vuex中的mapState

代码语言:javascript
复制
import { mapState } from 'vuex'

computed: mapState({
  article: state => state.articles.find(article => article.id == this.$route.params.id)
})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53414922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档