Vue.js从另一个组件

时间:2016-04-15 09:09:54

标签: javascript vue.js

我有两个组成部分:A和B.

组件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.

我对此一无所知。非常感谢你!!!。

1 个答案:

答案 0 :(得分: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

现在您必须解决如何从B Vue实例获取对A实例的引用,因为B <中可能有多个A组件/ p>