uni-app中uni.$on()无法接收到数据

496 阅读1分钟

最近在维护uni-app的一个项目,然后使用了uni.emit(事件,参数对象)发射事件,又用uni.emit('事件名', 参数对象)发射事件,又用uni.on('事件名', 接收的参数对象)来接收参数。 但是我发现在执行的时候,始终无法触发uni.on()方法,经过努力后,才在网友的帮助下,发现如果页面没有打开的话,那么uni.on()方法,经过努力后,才在网友的帮助下,发现如果页面没有打开的话,那么uni.emit()方法就无法触发,自然uni.on()也就无法触发,然后我在发射事件的函数中,使用了setTimeOut来延迟发射该事件,结果在另一个页面中成功的使用uni.on()也就无法触发,然后我在发射事件的函数中,使用了setTimeOut来延迟发射该事件,结果在另一个页面中成功的使用uni.on()接收到了该事件传来的参数。 代码如下: A页面:

doTask(task, index) {
    uni.navigateTo({
        url: `/pages/patrol/task/manifest-list?taskId=${task.id}&state=${task.state}`,
	success: res => {
            // 通过eventChannel向被打开页面传送检查清单列表id 
            if (this.history) {
                // res.eventChannel.emit('patrolId', task.id);
                setTimeout(function() {
                  uni.$emit('patrolId', task.id);
                }, 100)
            } else {
                // res.eventChannel.emit('manifest', task.inspectDetails);
                setTimeout(function() {
                  uni.$emit('manifest', task.inspectDetails)
                }, 100)
            }
	},
        fail: err => {
          console.log(err);
        },
    });
}

B页面接收:

onLoad(option) {
   uni.$once('patrolId', data => {
        this.history = true;
        this.taskId = data;
        console.log('taskId:', data)
        this.fetchDetail();
   })
    uni.$once('manifest', objData => {
        this.history = false;
        this.manifest = objData;
        console.log('manifest:', objData)
        this.fetchInspectList();
    })
}

当然这里面我也有不太懂的地方,也希望能有大神留言解释一番,比如: 我使用uni.navigateTo进行跳转后,success中会返回一个errMsg:navigateTo:ok,这是作何解释?还有为什么this.getOpenerEventChannel()这个函数在HbuilderX2.7.14版本中会报一个is not a function,虽然我知道文档中提示要在2.8.x+版本中使用,但是这又牵扯出了另一个问题,就是编译ts的时候会报一堆错误,而ts的编译插件用的是1.0.5版本,害....希望有大神看到后能够答疑解惑,谢谢大家