我希望你能够从我的Vue实例中更新组件中的数据。虽然有很多关于如何做相反操作的示例,但对于这个问题却没有找到什么资料。
比如说,我有以下代码:
Vue.component('component', {
props: {
prop: {
default: null
}
},
template: '#template',
data: function () {
return {
open: false
};
}
});
现在我想从我的Vue实例中将open
设置为true
:
var root = new Vue({
el: '#root',
data: {},
methods: { updateComponentData: function() {//set open to true} } });
root
)来更新子组件(例如你的示例中的component
)?如果是这样,强烈建议遵循一种模式,即从父组件向子组件传递数据,从子组件向父组件传递事件(更多信息请参见Vue文档)。在你的示例中,你的子组件将有一个名为open
的prop
,而你的父组件将把表示open
状态的值绑定到子组件上。 - Wingopen
设置为一个prop
,实际上你会收到Vue的警告,告诉你它应该是数据。你能否扩展你的问题,包括这个尝试?我不认为Vue会尝试警告你关于这个。 - Wingroot
不是一个组件。即component
不是root
的子级。root
是一个 Vue 实例。它们不是同一种东西。 - Green_qaue