简体   繁体   English

如何使用Vue对全局变量作出反应?

[英]How to react to a global variable with Vue?

I have a mobile webview that is injected with some global config object: 我有一个移动Web视图,其中注入了一些全局配置对象:

Vue.prototype.$configServer = {
  MODE: "DEBUG",
  isMobile: false,
  injected: false,
  version: -1,
  title:"App",
  user: null,
  host: "http://127.0.0.1:8080"
}

Later the WebView inject this: 后来,WebView将其注入:

Vue.prototype.$configServer = {
  MODE: "DEBUG",
  title: "App",
  version: "2.0",
  isMobile: true,
  injected: true,
  host: "http://127.0.0.1:8081"
}

And try to use it for the component: 并尝试将其用于组件:

const HomePage = {
  key: 'HomePage',
  template: '#HomePage',
  components: { toolbar },
  data() {
    return {
      items: [
        {name:"Login", link:"login"},
      ]
    }
  },
  computed:{
    config() {
      return Vue.prototype.$configServer
    }
  },
};

However the page is not updated. 但是,该页面未更新。 How react to the change? 如何应对变化?

PD: I confirm the object is updated with the safari debug tools. PD:我确认使用Safari调试工具更新了对象。 Also test in a local html. 还要在本地html中测试。

Instead of putting the config into the prototype of Vue, you can actually add it as a data option inside the main vue instance which will guarantee you that all your config properties will be reactive. 您实际上可以将配置作为数据选项添加到主vue实例中,而不必保证将配置放入Vue的原型中,这将确保您所有的配置属性都是反应性的。 As mentioned in the docs 如文档中所述

When you pass a plain JavaScript object to a Vue instance as its data option, Vue will walk through all of its properties and convert them to getter/setters using Object.defineProperty. 当您将纯JavaScript对象作为数据选项传递给Vue实例时,Vue将遍历其所有属性,并使用Object.defineProperty将它们转换为getter / setter。

Having said that whenever you update your config properties, vue will react to it. 话虽如此,只要您更新配置属性,vue都会对此做出反应。

Now let's see how to do it in code: 现在让我们看看如何在代码中做到这一点:

new Vue(){
    data:{ //only place where data is not a function
        configServer = {
              MODE: "DEBUG",
              isMobile: false,
              injected: false,
              version: -1,
              title:"App",
              user: null,
              host: "http://127.0.0.1:8080"
        } 
    }
}

Now whenever you need to access your config you can directly access it from any component using $root. 现在,每当需要访问配置时,都可以使用$ root从任何组件直接访问它。 Like this.$root.configServer . this.$root.configServer

Well that's it. 就是这样。

I hope it helps. 希望对您有所帮助。

There are 3 ways to acheive what you want 有3种方法来实现您想要的

1- Make sure you import vue in your component 1-确保在组件中导入vue

import 'Vue' from vue
...
...
...
 computed:{
    config() {
      return Vue.prototype.$configServer
    }

2- If you don't want to import vue the you can directly access prototype using proto from any instance. 2-如果您不想导入vue,则可以从任何实例使用proto直接访问原型。

computed:{
        config() {
          return this.__proto__.$configServer
        }

3- As you have added the config in the prototype you can actually access is directly from the vue instance using this 3-在原型中添加配置后,您实际上可以直接使用this实例从vue实例访问

computed:{
    config() {
      return this.$configServer
    }
  },

Well whatever style matches yours you can choose that. 好吧,无论哪种样式适合您,您都可以选择。

But I would personally recommend using the 3rd one, because accessing the prototype of instance is sort of an anti-pattern. 但是我个人建议使用第3个,因为访问实例的原型有点像反模式。

I hope it helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM