【发布时间】:2018-11-13 15:02:00
【问题描述】:
我将 $testCounter 放入一个插件中以使其全局化:
Vue.use({
install(Vue) {
Vue.prototype.$testCounter = 0;
Vue.prototype.$incrementCounter = () => {
Vue.prototype.$testCounter++;
};
});
我想在某个组件中输出它。我还需要在全球范围内以被动方式更新它的值:
<template>
<p>{{ $testCounter }}</p>
</template>
<script>
mounted() {
let comp = this;
comp.watcherId = setInterval(() => {
comp.$incrementCounter();
// I want to remove this line and still be reactive :
comp.$forceUpdate();
}, 1000);
}
</script>
我需要该属性具有反应性,我尝试了多种解决方案作为观察者、计算道具、vm.$set(...),但我找不到正确的方法。
【问题讨论】:
-
你可以使用 Vuex 吗?
-
我暂时不打算使用它,我避免为基本应用安装太多层
标签: vuejs2 vue-component vue-reactivity