微信小程序 this.triggerEvent()的具体使用
网络编程 2021-07-04 15:02www.168986.cn编程入门
这篇文章主要介绍了微信小程序 this.triggerEvent()的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们狼蚁网站SEO优化随着长沙网络推广来一起学习学习吧
在对组件进行封装时 在当前页面想要获取组件中的某一状态,需要使用到this.triggerEvent(' ',{},{}),第一个参数是自定义事件名称,这个名称是在页面调用组件时bind的名称,第二个对象就可以将想要的属性拿到,第三个参数文档中有介绍,有机会再做补充。
在这个demo中like组件是我要封装的组件,在classic.wxml中调用的:
组件like.vue的页面是这样写的:
<view bind:tap="onLike" class="container"> <image src="{{like?yesSrc:noSrc}}" /> <text>{{count}}</text> </view>
组件的like.js中methods是这样写的:
methods: { onLike(event) { let like = this.properties.like; let count = this.properties.count; count = like ? count - 1 : count + 1; this.setData({ like: !like, count }) let behavior = this.properties.like ? "like" : "cancel"; this.triggerEvent('like', { behavior }, {}) } }
那么在classic.wxml中调用组件:
这里要注意bind:后面的like是this.triggerEvent()的第一个参数,自定义事件名称
<v-like bind:like="onLike" like="{{classicData.like_status}}" count="{{classicData.fav_nums}}"/>
classic.js中onlike就可以实现页面对组件属性的获取:
onLike: function(event) { console.log(event) }
event.detail.behavior就可以拿到是不是喜欢的属性了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
上一篇:jquery实现弹窗(系统提示框)效果
下一篇:jQuery实现消息弹出框效果
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程