简体   繁体   English

Vue - 使 output 从全局 function 反应

[英]Vue - make output from global function reactive

I started using Vue some time ago, together with vue-cli and Single File Components.我前段时间开始使用 Vue,以及 vue-cli 和单文件组件。 I have a "problem", that I want to have a global function that returns formatted text to the components (used in most of the components in my app) depending on the current (global) settings of that "function" (or class).我有一个“问题”,我想要一个全局 function 根据该“函数”(或类)的当前(全局)设置向组件(在我的应用程序中的大多数组件中使用)返回格式化文本. I want it so that when the settings (in this example, currentKey) change, then all the components using this function get the value updated.我希望它在设置(在本例中为 currentKey)更改时,使用此 function 的所有组件都会更新值。 So in short: currentKey changes - text gets re-drawn to match the new return value from test global function.简而言之:currentKey 更改 - 重新绘制文本以匹配来自test全局 function 的新返回值。

There is some additional logic involved, but that's the simplest example I came up with.涉及一些额外的逻辑,但这是我想出的最简单的例子。

In the example you can see that there is a 5 second interval that cycles through the currentKey variable, therefore changing the output of the test function.在示例中,您可以看到循环通过 currentKey 变量的间隔为 5 秒,因此更改了test function 的 output。 I'd want the components to get updated accordingly each 5 seconds.我希望组件每 5 秒相应更新一次。 I tried using computed values and other stuff I found, but couldn't get it to work the way I want.我尝试使用计算值和我找到的其他东西,但无法让它以我想要的方式工作。

How can I force the components to update whenever I change the currentKey variable?每当我更改 currentKey 变量时,如何强制组件更新?

 Vue.component('component1', { template: '<div>{{ $test("name") }}</div>', }); Vue.component('component2', { template: '<div>{{ $test("name2") }}</div>', }); var table = { keyone: { name: 'TEST NAME FROM FIRST KEY', name2: 'TEST NAME 2 FROM FIRST KEY', }, keytwo: { name: 'TEST NAME FROM <b>SECOND</b> KEY', name2: 'TEST NAME 2 FROM <b>SECOND</b> KEY', } }; var currentKey = 'keyone'; Vue.prototype.$test = function(name) { return table[currentKey][name]; }; setInterval(function() { if(currentKey == 'keyone') currentKey = 'keytwo'; else currentKey = 'keyone'; console.log('Key changed to', currentKey); }, 5000); new Vue({ el: '#app', });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <component1></component1> <component2></component2> </div>

Reactivity is all about the properties of objects.反应性是关于对象的属性。 When an object is observable Vue can track the reading and writing of its properties.当 object 是可观察的时,Vue 可以跟踪其属性的读取和写入。

So all you need to do is make currentKey a property on an observable object and all the reactivity magic will kick in.所以你需要做的就是让currentKey成为一个可观察的 object 的属性,所有的反应魔法都会发挥作用。

So long as you're using Vue 2.6 you can use Vue.observable directly to create an observable object.只要你使用的是 Vue 2.6,你就可以直接使用Vue.observable来创建一个 observable object。 In earlier versions you'd need to create a dummy Vue instance and use the data function to apply reactivity to the object instead.在早期版本中,您需要创建一个虚拟 Vue 实例并使用data function 来对 object 应用反应性。

 Vue.component('component1', { template: '<div>{{ $test("name") }}</div>', }); Vue.component('component2', { template: '<div>{{ $test("name2") }}</div>', }); var table = { keyone: { name: 'TEST NAME FROM FIRST KEY', name2: 'TEST NAME 2 FROM FIRST KEY', }, keytwo: { name: 'TEST NAME FROM <b>SECOND</b> KEY', name2: 'TEST NAME 2 FROM <b>SECOND</b> KEY', } }; var config = Vue.observable({ currentKey: 'keyone' }); Vue.prototype.$test = function(name) { return table[config.currentKey][name]; }; setInterval(function() { if(config.currentKey == 'keyone') config.currentKey = 'keytwo'; else config.currentKey = 'keyone'; console.log('Key changed to', config.currentKey); }, 5000); new Vue({ el: '#app', });
 <script src="https://unpkg.com/vue@2.6.10/dist/vue.js"></script> <div id="app"> <component1></component1> <component2></component2> </div>

The example only includes rendering updates but any computed properties would also be triggered, just like they would with data property changes.该示例仅包括渲染更新,但也将触发任何计算属性,就像data属性更改一样。

https://v2.vuejs.org/v2/api/#Vue-observable https://v2.vuejs.org/v2/api/#Vue-observable

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

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