当前位置:   article > 正文

微信小程序自定义组件同步调用_triggerevent 微信小程序

triggerevent 微信小程序

目前微信官方的推荐写法一般都是用triggerEvent来抛出数据,用bind...来接收

这里提供组件的同步调用不适用bind来接收triggerEvent抛出的数据

例子:自定义弹窗,同步调用自定义弹窗组件返回当前用户操作结果

(借助了co.js,不想使用的可以用async和await代替)

组件内实现:

  1. const regeneratorRuntime = require('/lib/co/runtime')
  2. const co = require('/lib/co/co')
  3. Component({
  4. successfun:null,
  5. showPopSync:co.wrap(function*(obj={}){
  6. let that=this
  7. return new Promise((resolve, reject) => {
  8. that.successfun= function(res) {
  9. resolve(res)
  10. }
  11. that.showModal(obj)
  12. })
  13. }),
  14. //这里执行数据初始化例如设置弹窗内容什么的
  15. showModal:co.wrap(function*(obj){
  16. }),
  17. //用户在页面内点击了确认按钮
  18. userConfirm:function(){
  19. that.successfun({
  20. confirm:true
  21. })
  22. },
  23. //用户在页面内点击了取消按钮
  24. userCancel:function(){
  25. that.successfun({
  26. cancel:true
  27. })
  28. },
  29. })

调用组件页面内实现:

wxml:

<modal id="modal" />

js:

  1. Page({
  2. showSelfModal:co.wrap(function*(){
  3. let res=yield this.selectComponent("#modal").showPopSync({
  4. title:"提示"
  5. })
  6. if(res.confirm){
  7. //用户确认弹窗内容
  8. }
  9. })
  10. })

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/298494
推荐阅读
相关标签
  

闽ICP备14008679号