Vue.js 从另一个组件调用方法

Vue.js Call method from another component

我有两个组成部分。如何在 createProject() 方法中调用 fetchProjectList() 方法。

第一部分:

Vue.component('projects', {
    template: '#projects-template',

    data: function () {
        return {
            list: []
        }
    },

    ready: function () {
        this.fetchProjectList();
    },

    methods: {
        fetchProjectList: function () {
            resource.get().then(function (projects) {
                this.list = projects.data;
            }.bind(this));
        }
    }

});

第二部分

Vue.component('createProjects', {
    template: '#create-projects-template',

    methods: {
        createProject: function () {
            resource.save({}, {name: this.name}).then(function () {
                this.fetchProjectList()
            }.bind(this), function (response) {
                // error callback
            });
        }
    }
});

你没有,或者说你不应该。组件不应该以这种直接的方式依赖于其他组件。

您应该将此方法提取到 mixin 中,或者将其保留在您导入到每个组件中的自己的对象中。

阅读商店模式:http://vuejs.org/guide/application.html#State_Management