// Child Component
var TestComp = Vue.component('TestComp', {
template: `<div>Child Component Prop Value : {{text}} </div>`,
// child component prop --> text
props: {
text: {
default: 'init prop'
}
}
});
// Parent
var vm = window.vm = new Vue({
el: "#root",
template: "#app",
mounted() {
this.createSubComp();
},
data() {return {}},
methods: {
createSubComp() {
var that = this;
var copy = Vue.extend(TestComp);
var instance = window.vmsub = new copy({
// here, remove comment
// parent: that
});
// add or remove comment above, cause different result
instance.text = 'pass child component prop';
instance.$mount();
this.$el.append(instance.$el);
}
},
components: {
TestComp
}
});
<script src="https://vuejs.org/js/vue.min.js"></script>
<div id="root"></div>
<script type="text/x-template" id="app">
<div style="height: 100; border: 1px solid red; overflow: auto;">
</div>
</script>
答案 0 :(得分:0)
我认为您使用Vue组件的方法看起来像一些变通办法,可以实现非常特殊的功能...
但是,如果不是这种情况,则更简单的Vue实现方法看起来更像这样,只需让Vue.js进行其构建即可:
// Child Component
var TestComp = Vue.component('TestComp', {
template: `<div>Child Component Prop Value : {{text}} </div>`,
// child component prop --> text
props: {
text: {
default: 'init prop'
}
}
});
// Parent
var vm = window.vm = new Vue({
el: "#root",
template: '<test-comp text="pass child component props" />', // passing props to child component, using template
data() {return {}},
components: {
TestComp
}
});
<script src="https://vuejs.org/js/vue.min.js"></script>
<div id="root"></div>
<script type="text/x-template" id="app">
<div style="height: 100; border: 1px solid red; overflow: auto;">
</div>
</script>
Vue.js将为您创建子组件