【问题标题】:How to update vue-meta on route/url change?如何在路由/网址更改时更新 vue-meta?
【发布时间】:2020-08-03 09:38:45
【问题描述】:

当路线改变时,我网站上的元数据没有更新。路线本身有一个watch,它可以很好地更新视图,但是来自vue-metametaInfo() 跟不上。我的代码的<script> 部分如下所示:

<script>
    export default {
        name: "Product",
        watch: {
            '$route.params.ProductID': {
                deep: true,
                immediate: true,
                handler() {
                    this.getProduct(); // calls getProduct() on route change. Can I also call metaInfo() from here somehow?
                }
            }
        },
        metaInfo() {
            return {
            title: this.Product.ProductTitle,
            meta: [
                {
                    name: 'description', content: this.Product.ProductTitle
                }
            ]
        }
        },
        computed: {
            Product() {
                return this.$store.getters.getProduct
            }
        }, mounted() {
            if (this.Product == null || !this.Product.length) {
                this.getProduct();
            }
        }, methods: {
            getProduct() {
                return this.$store.dispatch('loadProduct', {ProductID: this.$route.params.ProductID})

            } 
        }
    }
</script>

发生的情况是,当我更改路线并从 /product/123 转到 /product/124 时,metaInfo() 仍然显示 /product/123 的元数据。如果我点击刷新,metaInfo() 会更新并显示/product/124 的正确数据。

我需要watch 来触发metaInfo() 的更新,但不知道该怎么做。我在任何地方的文档中都找不到此信息。请帮忙?

【问题讨论】:

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


【解决方案1】:

对于响应式,在 return 语句之外使用变量。

metaInfo() {
    const title = this.Product.ProductTitle;

    return {
        title: title,
        meta: [
            {
                name: 'description', content: title
            }
        ]
    }
}

https://vue-meta.nuxtjs.org/guide/caveats.html#reactive-variables-in-template-functions

【讨论】:

    猜你喜欢
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 2019-03-28
    • 2021-02-06
    • 2019-06-23
    • 1970-01-01
    相关资源
    最近更新 更多