2016-04-15 104 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