window跟vue变量互相绑定

js实现变量监听

    //定义一个对象,挂载到window下,后续在任何模块中,给这个对象的show属性赋值,都将触发set对应的代码,我这么写主要是为了解决vue子组件向父组件传值的问题
    window.mlistener={}
    let _this = this
    Object.defineProperty(mlistener,'show',{
      set: function(val){
        _this.showHeader = val
        _this.showFoot = val
      }
    })

window变量触发vue渲染

可保持window变量,跟vue用到的变量同步更新

//将window赋值给vue变量 
   data() {
      return {
        tableH: 0,
        isShow: true,
        filterText: '',
        window: window,
        tree_data: [],
        expendKeys: [],
        defaultProps: {
          children: 'children',
          label: 'label'
        }
      }

//用法
{{window.getTest()}}


//getTest()
    window.getTest = ()=>{
        console.dir(this.fullScreen)
        return this.fullScreen
    }

结果

参考

js监听一个变量的变化

原文地址:https://www.cnblogs.com/lurenjia1994/p/10006619.html