【问题标题】:Using vue-meta with computed properties使用带有计算属性的 vue-meta
【发布时间】:2019-04-24 04:35:10
【问题描述】:

我正在尝试将动态元标记添加到我的文章页面。文章存储在 VueX 存储中,我使用计算属性来获取要显示的文章:

computed: {
  article() {
   return this.$store.state.articles.filter(article => article.id == this.$route.params.id);
  }
}

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

正确的使用方法是:

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() 时才有效。既然组件返回的是动态文章,那么如何在计算属性中访问过滤后的文章呢?

谢谢你的帮助

【问题讨论】:

    标签: javascript vue.js vue-meta


    【解决方案1】:

    你需要将你的计算属性命名为article,还要感谢@ssc-hrep2 使用find 而不是filter 在数组中返回匹配的对象,因为filter 返回一个数组:

    computed: {
      article () {
        return this.$store.state.articles.find(article => article.id == this.$route.params.id)
      }
    }
    

    或者使用mapState from vuex:

    import { mapState } from 'vuex'
    
    computed: mapState({
      article: state => state.articles.find(article => article.id == this.$route.params.id)
    })
    

    【讨论】:

    • 我做到了,我只是拼错了,我会编辑谢谢
    • @Psidom 我正在使用 vue-meta 为“谷歌富卡”动态更新 application/ld+json 我可以在检查元素时看到它想知道是否有办法在查看源代码谢谢
    猜你喜欢
    • 2021-07-23
    • 1970-01-01
    • 2020-02-03
    • 2020-01-25
    • 2021-12-17
    • 2019-10-25
    • 2020-12-01
    • 2021-05-04
    • 2022-11-11
    相关资源
    最近更新 更多