taro 小程序 & touch event 转换 bug
taro 小程序 & touch event 转换 bug
before
after
事件处理
https://nervjs.github.io/taro/docs/event.html#docsNav
阻止事件冒泡
在 Taro 中另一个不同是你不能使用 catchEvent 的方式阻止事件冒泡。
你必须明确的使用 stopPropagation。
例如,阻止事件冒泡你可以这样写:
class Toggle extends Component {
constructor (props) {
super(props)
this.state = {isToggleOn: true}
}
onClick = (e) => {
e.stopPropagation()
this.setState(prevState => ({
isToggleOn: !prevState.isToggleOn
}))
}
render () {
return (
<button onClick={this.onClick}>
{this.state.isToggleOn ? 'ON' : 'OFF'}
</button>
)
}
}
除了 bind 之外,事件参数也可以使用匿名函数进行传参。
直接写匿名函数不会打乱原有监听函数的参数顺序:
class Popper extends Component {
constructor () {
super(...arguments)
this.state = { name: 'Hello world!' }
}
render () {
const name = 'test'
return (
<Button onClick={(e) => {
e.stopPropagation()
this.setState({
name
})
}}>
{this.state.name}
</Button>
)
}
}
注意: 使用通过 usingComponents 的第三方组件不支持匿名函数
事件系统
事件分类
事件分为冒泡事件和非冒泡事件:
冒泡事件:当一个组件上的事件被触发后,该事件会向父节点传递。
非冒泡事件:当一个组件上的事件被触发后,该事件不会向父节点传递。
https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxml/event.html#事件详解
绑定并阻止事件冒泡
除 bind 外,也可以用 catch 来绑定事件。与 bind 不同, catch 会阻止事件向上冒泡。
事件对象
事件类型
事件分为冒泡事件和非冒泡事件:
冒泡事件:以关键字 on 为前缀,当组件上的事件被触发,该事件会向父节点传递。
非冒泡事件:以关键字 catch 为前缀,当组件上的事件被触发,该事件不会向父节点传递。
https://opendocs.alipay.com/mini/framework/events
https://opendocs.alipay.com/mini/framework/event-object
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/12704710.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2019-04-15 table index & delete array item
2019-04-15 js splice vs slice
2019-04-15 iview render bug & vue namespace bug All In One
2019-04-15 shit iview docs & i-radio bug
2019-04-15 windows 10 & task view & shortcut
2019-04-15 Postman & API