UNI-APP_uni-app子组件调用父组件方法

父组件

引入组件header

<Header  @call="call" />

父组件methods添加方法

call(){
    
     
	console.log("子组件调用了父组件方法")
}
//接受参数写法
//call(type){ 
//	console.log("子组件调用了父组件方法",type)
//}

子组件

页面添加点击事件

<view @click="changeType">调用父组件方法</view>

子组件methods中添加方法,使用$emit调用父组件方法

changeType(){
    
    
	this.$emit("call")
	//传入参数写法
	//this.$emit("call",type)
},

注意:$emit()不能触发父组件方法的原因

$emit传入的事件名称只能使用小写,不能使用大写的驼峰规则命名
如果修改后还是不行的话,就改用:
this.$parent.Event (Event为父组件中的自定义方法)

猜你喜欢

转载自blog.csdn.net/weixin_44599931/article/details/108844695