学无先后,达者为师

网站首页 编程语言 正文

微信小程序 - 子组件触发父组件函数(triggerEvent)

作者:王佳斌 更新时间: 2022-02-27 编程语言

前言

类似 Vuethis.$emit(),子组件可执行父组件的函数方法。

在小程序里,组件间通信与传统 Vue 有所区别。

解决方案

官方文档:https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/events.html

其实官方文档已介绍 this.triggerEvent 与其参数,下面是更加易于理解的例子。


子组件(help):

<view bindtap="onTap">点我触发父组件函数方法</view>
properties:{ text: String },
methods:{
    onTap(event) {
        this.triggerEvent("tapping", {
            text: this.propertie.text
        },{})
    }
}

父组件:

<help text="ok" bind:tapping="onPost" />
onPost(event) {
	console.log(event.detail.text)
    console.log('当onTap执行时我就执行了')
}

写在后面

当然,该示例只是让您易于理解,更多写法及注意事项请参考官方文档。

原文链接:https://wangjiabin.blog.csdn.net/article/details/122981498

栏目分类
最近更新