Vue 监听其他js文件变量的变化?
改变样式,通过msg布尔值
1.can.js
var obj = {},initValue = true;
Object.defineProperty(obj,“newKey”,{
get:function (){
return initValue;
},
set:function (value){
initValue = value;
}
});
点击改变布尔值为false
document.getElementById('btncc').onclick = function () {
initValue = false;
console.log(123);
}
通过延时器我已经知道,已被改变
setTimeout(()=>{
console.log( obj.newKey )
},3000)
抛出我所需要的函数和对象,然后将obj.newKey赋予msg,然后通过watch监听,看是否有变化,但是这里的监听好像没起作用
export default {
con,obj
}
2.vue
import Canvas from '…/…/Api/js’
export default {
name: ‘HelloWorld’,
data () {
return {
msg: Canvas.obj.newKey
}
},
mounted () {
Canvas.con()
},
watch: {
‘obj.newKey’ (val) {
console.log(val);
}
}
}
我就是想通过can.js改变的布尔值传递给vue页面,只要改变vue页面就能接到,现在好像是can.js改变了,vue页面不知道,请问这种情况怎么办?