组件
<template>
<button>
1
</button>
</template>
1. 导入
import CounterCom from './components/CounterCom.vue'
2. 注册
components:{CounterCom}
3. 使用
<CounterCom></CounterCom>
父传子 props
App.vue文件中
<CounterCom :num="1"></CounterCom>
1. 接收参数
props: {
'num':{type:Number,default}
}
2. 使用参数num
data() {
return {
counter: this.num
}
}
子传父 $emit
CounterCom.vue
<button @click="counter++;$emit('counterchange',counter)"></button>
App.vue
<CounterCom @counterchange="n=$event"></CounterCom>
$emit('事件名',事件值) 发送一次时间,事件名(counterchange)和事件值(counter)是自定义的
$event固定写法,事件的值(counterchange 事件的值,也是子组件$emit的第二个参数)