2017-08-07 69 views
1

我的根看起来像这样。我已经在数据和监视中定义了类型和历史变量,我试图访问另一个变量而不是我正在观察的变量。试图访问外.map()历史作品可变然而用它里面.map()回报undefined不能在watcher内部访问数据变量.map()

new Vue({ 
    el: '#root', 

    data: { 
    types: {}, 
    history: {} 
    } 

    watch: { 
    types: { 
     handler(obj) { 
      console.log(this.history) // works 

      types.map(function(val) { 
       console.log(this.history) // undefined 
      }) 
     } 
    } 
    } 

} 
+0

您需要使用ES6语法,尝试'types.map((VAL)=> {...})'假设你使用的WebPack或东西来编译JS –

回答

1

由于您使用的是CDN,您将需要绑定this给你的函数。请尝试以下操作:

types.map(function(val) { 
    console.log(this.history) 
}).bind(this); 
+0

我试着'types.map((val)=> {...}}'它工作正常。非常感谢! – senty