2016-04-15 57 views
1

获取数据我有2个组件:A和B.Vue.js来自另一组件

组分A:

import B from '../components/B.vue'; 

export default { 

    components: { 
     B 
    }, 

    methods: { 
     test: function() { 
      console.log(B.data().settings); 
     } 
    } 
} 

和组分B:

export default { 

    data() { 
     return { 
      setting: '123' 
     } 
    } 

} 

当我触发测试方法那么我得到的值是123.但是,当我从输入中输入新值并再次触发测试方法时,我无法获得新值,值仍然为123.

我不知道这件事。非常感谢 !!!。

回答

2

您正在执行组件定义的数据功能。要从组件的实例中获得所需的值,只需调用所需的数据即可。例如:

import B from '../components/B.vue'; 

// as I say, work on instance of Vue, not in components definitions 
let b = new B() 

console.log(b.settings) // logs the settings for that instance, this value is reactive 
console.log(b.$data) // log all the current data for that component, also reactive 
console.log(B.data()) // log the definition data, not reactive 

现在,你必须解决如何让从A Vue的实例的B实例的引用,因为可能有不止一个B成分A