【发布时间】:2020-01-08 07:14:25
【问题描述】:
我想从父组件设置动态组件的数据
例如: 父组件:
<div id="app">
<template v-for="(component, index) in components">
<component :is="component" :key="index"></component>
</template>
<button @click="add()">Add Component</button>
</div>
动态组件:
let dynamicComponent = {
template: `
<p>Welcome {{ msg }}!</p>
`,
data () {
return {
msg: 'home'
}
},
}
const App = new Vue({
el: '#app',
data: {
components: [
dynamicComponent
]
},
methods: {
add () {
this.components.push(dynamicComponent);
},
}
});
我想在添加新的动态组件时从父组件设置动态组件的数据。
在这种情况下,父组件中dynamicComponent的msg属性
【问题讨论】: