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監聽一個變量的變化


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM