How to use a Vue variable in metaInfo?

别等时光非礼了梦想. 提交于 2020-03-04 16:39:06

问题


What is the correct syntax for this line Vue inside the script:

export default {
metaInfo: {
      title: "{product.name} wallpaper",

The line came to the form:

title: "Space wallpaper",

product.name I get it in api:

mounted() 
{ 
let vm = this
vm.getProducts(); 

},
methods: {
getProducts() {
                let vm = this
                axios.get('/api/products')
                    .then(function(response) {
                        vm.products = response.data.data  
                    })

回答1:


Template literals use backticks and a dollar sign before the braces, e.g.:

title: `${product.name} wallpaper`

To use reactive variables in the metaInfo as part of a function, the Vue Meta docs says that you can make metaInfo a function and assign the reactive variable to a local variable before returning the data. For example:

metaInfo() {
      const product = this.product;
      return {
          title: `${product.name} wallpaper`
      }
}

One of the contributors to Vue Meta explained why you need to do it this way here.



来源:https://stackoverflow.com/questions/59629685/how-to-use-a-vue-variable-in-metainfo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!