javascript Vue 3全局属性在已安装或已计算中不可见

wi3ka0sx  于 2022-12-17  发布在  Java
关注(0)|答案(1)|浏览(141)

我在main.js中设置了一个属性:

app.config.globalProperties.$backendUrl  = "https://myhost:8989";

然后,当将其放入组件时,我遇到以下情况:

methods: {
     save() {
        console.log(this.$backendUrl); //outputs CORRECT result
     }
  },

  mounted() {
     console.log(this.$backendUrl); //outputs undefined
  },

  computed: {
     treeData(){
        console.log(this.$backendUrl); //outputs undefined
     }
  }

怎么了?

i34xakig

i34xakig1#

main.js中如何定义app
也就是说,您不应该 * 做 * 例如:

// app is component instance
const app = createApp(App).use(router).mount("#app");

app.config.globalProperties.$backendUrl = "https://myhost:8989";

相反,您应该:

// app is app instance
const app = createApp(App)
app.use(router).mount("#app");

app.config.globalProperties.$backendUrl = "https://myhost:8989";

相关问题