微信小程序 eventChannel.emit is not a function;at “pageswxmlindex“ page lifeCycleMethod onLoad function

tech2022-09-14  100

欢迎大家访问我的博客 blog.ayla1688.cool


原文链接: http://blog.ayla1688.cool/archives/293.html

在传参的过程中,遇到报错 ``` eventChannel.emit is not a function;at "pages/wxml/index" page lifeCycleMethod onLoad function ```

在index.js 中使用 navigateTo 跳转到 logs.js , 在index.js中监听 logs.js 中传递的参数发生报错

// index.js ``` //index.js //获取应用实例 const app = getApp()

wx.navigateTo({   url: '/pages/logs/logs?id=111&query=test',   events: {     // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据     acceptDataFromOpenedPage: function(data) {       console.log(data)     },     someEvent: function(data) {       console.log(data.data)     }   },   success: function(res) {     // 通过eventChannel向被打开页面传送数据     res.eventChannel.emit('acceptDataFromOpenerPage', { data: 'test' })   } })

Page({   data: {     motto: 'Hello World',     userInfo: {},     hasUserInfo: false,     canIUse: wx.canIUse('button.open-type.getUserInfo')   },   //事件处理函数   bindViewTap: function() {     wx.navigateTo({       url: '../logs/logs'     })   },   onLoad: function () {          if (app.globalData.userInfo) {       this.setData({         userInfo: app.globalData.userInfo,         hasUserInfo: true       })     } else if (this.data.canIUse){       // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回       // 所以此处加入 callback 以防止这种情况       app.userInfoReadyCallback = res => {         this.setData({           userInfo: res.userInfo,           hasUserInfo: true         })       }     } else {       // 在没有 open-type=getUserInfo 版本的兼容处理       wx.getUserInfo({         success: res => {           app.globalData.userInfo = res.userInfo           this.setData({             userInfo: res.userInfo,             hasUserInfo: true           })         }       })     }   },   getUserInfo: function(e) {     console.log(e)     app.globalData.userInfo = e.detail.userInfo     this.setData({       userInfo: e.detail.userInfo,       hasUserInfo: true     })   }, })

```

logs.js

``` //logs.js const util = require('../../utils/util.js')

Page({   data: {     logs: []   },   onLoad: function (options) {     console.log('logs.js 收到了', options.id)     const ec = this.getOpenerEventChannel()     console.log(ec)     ec.emit('someEvent', {data: 'i know'})   } })

```

使用eventChannel 从 ```to ``` 页面向 ```from ``` 页面传递数据

解决办法 ```index.js``` 中 wx.navigateTo 定义的地方错误, 如果放入onLoad 就没有问题

正确 ``` //index.js //获取应用实例 const app = getApp()

Page({   data: {     motto: 'Hello World',     userInfo: {},     hasUserInfo: false,     canIUse: wx.canIUse('button.open-type.getUserInfo')   },   //事件处理函数   bindViewTap: function() {     wx.navigateTo({       url: '../logs/logs'     })   },   onLoad: function () {     wx.navigateTo({       url: '/pages/logs/logs?id=111&query=test',       events: {         // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据         acceptDataFromOpenedPage: function(data) {           console.log(data)         },         someEvent: function(data) {           console.log(data.data)         }       },       success: function(res) {         // 通过eventChannel向被打开页面传送数据         res.eventChannel.emit('acceptDataFromOpenerPage', { data: 'test' })       }     })     if (app.globalData.userInfo) {       this.setData({         userInfo: app.globalData.userInfo,         hasUserInfo: true       })     } else if (this.data.canIUse){       // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回       // 所以此处加入 callback 以防止这种情况       app.userInfoReadyCallback = res => {         this.setData({           userInfo: res.userInfo,           hasUserInfo: true         })       }     } else {       // 在没有 open-type=getUserInfo 版本的兼容处理       wx.getUserInfo({         success: res => {           app.globalData.userInfo = res.userInfo           this.setData({             userInfo: res.userInfo,             hasUserInfo: true           })         }       })     }   },   getUserInfo: function(e) {     console.log(e)     app.globalData.userInfo = e.detail.userInfo     this.setData({       userInfo: e.detail.userInfo,       hasUserInfo: true     })   }, })

````

 

最新回复(0)