通过组件的 e m i t 、 emit、 emit、on 方法
// 在父组件,首先使用ref标记组件
<children ref="children"></children>;
// 触发子组件中注册的事件
this.$refs.children.$emit("test");
// 在子组件,绑定事件,在调用事件之前要注册好
this.$on("test", (val) => {
this.func(val); // 你要触发的子组件中的函数
});
通过 ref 直接调用子组件的方法;
// 在父组件,首先使用ref标记组件
<children ref="children"></children>
this.$refs.children.test();
// 在子组件
...
methods:{
test(){}
}
子组件通过$emit;
// 在父组件,首先事件
<children @custom="handle"></children>
methods:{
handle(data){
// do something
}
}
// 子组件中
this.$emit('custom',{xx:xx});// 这样可以触发父组件的custom事件
子组件通过prop传回调函数
// 父组件
<children :callback="handle"></children>
methods:{
handle(data){
// do something
}
}
// 子组件
props:{
callback: {
type: Function,
default: null
},
}
// 某个地方去触发
this.callback && this.callback({xx:xx});
还有什么其他方式可以触发的大家可以评论留言