赞
踩
目前微信官方的推荐写法一般都是用triggerEvent来抛出数据,用bind...来接收
这里提供组件的同步调用不适用bind来接收triggerEvent抛出的数据
例子:自定义弹窗,同步调用自定义弹窗组件返回当前用户操作结果
(借助了co.js,不想使用的可以用async和await代替)
组件内实现:
- const regeneratorRuntime = require('/lib/co/runtime')
- const co = require('/lib/co/co')
- Component({
- successfun:null,
- showPopSync:co.wrap(function*(obj={}){
- let that=this
- return new Promise((resolve, reject) => {
- that.successfun= function(res) {
- resolve(res)
- }
- that.showModal(obj)
- })
- }),
-
- //这里执行数据初始化例如设置弹窗内容什么的
- showModal:co.wrap(function*(obj){
-
- }),
-
- //用户在页面内点击了确认按钮
- userConfirm:function(){
- that.successfun({
- confirm:true
- })
- },
-
- //用户在页面内点击了取消按钮
- userCancel:function(){
- that.successfun({
- cancel:true
- })
- },
- })
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
调用组件页面内实现:
wxml:
<modal id="modal" />
js:
- Page({
- showSelfModal:co.wrap(function*(){
- let res=yield this.selectComponent("#modal").showPopSync({
- title:"提示"
- })
- if(res.confirm){
- //用户确认弹窗内容
- }
- })
- })
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。