Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:组件之间的通信,其实还有其它方式的,有空多看看vue官网
组件是可复用的vue实例,从形式上看就是一个自定义html标签
模板里面第一级只能有一个标签,不能并行;
//声明
Vue.component("child-component", childComponent{});
const vm = new Vue({})
const vm = new Vue({
el: ".app",
components: {
MyClick: {
template: `<h1>我是局部组件</h1>`,
},
},
});
<div class="app">
//用子组件自定义属性my-age获取父组件age的值
<my-click :my-age="age"></my-click>
</div>
<script>
const vm = new Vue({
el: ".app",
data() {
return {
age: 28,
};
},
//声明子组件
components: {
myClick: {
props: ["myAge"],
template: `<p>我的年龄是{{myAge}}</p>`,
},
},
});
</script>
<div class="app">
//用子组件@click-count绑定的事件调用父组件handle方法
<my-click :my-age="age" :my-count="count" @click-count="handle"></my-click>
</div>
<script>
const vm = new Vue({
el: ".app",
data() {
return {
age: 28,
count: 0,
};
},
components: {
myClick: {
props: ["myAge", "myCount"],
template: `
<div>
<p>我的年龄是{{myAge}}</p>
<button @click="$emit('click-count',1)">click:+{{myCount}}</button>
</div>
`,
},
},
methods: {
handle(value) {
this.count += value;
console.log(this.count);
},
},
});
</script>