n***@tom.com
n***@tom.com
  • 发布:2022-12-11 11:56
  • 更新:2023-12-19 11:45
  • 阅读:250

【报Bug】eventChannel.on中无法修改当前页面data中的值

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Mac

PC开发环境操作系统版本号: mojave

HBuilderX类型: 正式

HBuilderX版本号: 3.6.5

手机系统: Android

手机系统版本号: Android 12

手机厂商: 小米

手机机型: 红米k40s

页面类型: vue

vue版本: vue2

打包方式: 云端

项目创建方式: HBuilderX

示例代码:
onLoad() {  
    const eventChannel = this.getOpenerEventChannel();  
    eventChannel.on('xxx', (res) => {  
        this.data1 = 'abc' //这行没有任何效果  
    })  
}

操作步骤:
onLoad() {  
    const eventChannel = this.getOpenerEventChannel();  
    eventChannel.on('xxx', (res) => {  
        this.data1 = 'abc' //这行没有任何效果  
    })  
}

预期结果:
onLoad() {  
    const eventChannel = this.getOpenerEventChannel();  
    eventChannel.on('xxx', (res) => {  
        this.data1 = 'abc' //这行有效果  
    })  
}

实际结果:
onLoad() {  
    const eventChannel = this.getOpenerEventChannel();  
    eventChannel.on('xxx', (res) => {  
        this.data1 = 'abc' //这行没有任何效果  
    })  
}

bug描述:

eventChannel.on中无法修改当前页面data中的值,例如:

onLoad() {  
    const eventChannel = this.getOpenerEventChannel();  
    eventChannel.on('xxx', (res) => {  
        this.data1 = 'abc' //这行没有任何效果  
    })  
}

这种情况下,使用了很多办法都不行,比如在eventChannel.on之前定义let that = this,都不行

但是在NVUE下,同样的代码就可以,例如:

    onLoad() {        
        const eventChannel = this.$scope.eventChannel;            
        eventChannel.on('xxx', (res)=> {  
            this.data1 = 'abc' //这样是可以的  
        })        

    }   
2022-12-11 11:56 负责人:无 分享
已邀请:
DCloud_UNI_WZF

DCloud_UNI_WZF

测试未复现该问题,请检查代码逻辑或提供测试工程

w***@icloud.com

w***@icloud.com - 拥抱变化,迭代频繁

无法在on内直接给data赋值,因为此时this指向并非全局,可以使用局部变量接收值,将赋予data

            let obj = {}  
            eventChannel.on('acceptDataFromOpenerPage', function(data) {  
                obj = {...data.data}  
            })  
            this.standardName = obj.standardName

要回复问题请先登录注册