父组件:
<Student ref="student" />
methods:{
getName(name){
console.log("学生名:",name)
}
}
mounted(){
//当自定义事件sName被触发时
this.$refs.student.$on('sName',this.getName)
}
子组件:
<button @click="sendName">将学生名传递给父组件</button>
<button @click="unbind">解绑自定义事件</button>
<button @click="death">销毁当前student组件的实例(vc)</button>
methods:{
sendName(){
this.$emit("sName",this.name)
}
unbind(){
this.$off('sName') //解绑一个
this.$off(['sName','demo']) //解绑多个
this.$off() //所有自定义事件全部解绑
}
death(){
this.$destroy()
//销毁当前组件的实例,销毁后所有student组件实例的自定义事件全都不奏效
}
}