0
点赞
收藏
分享

微信扫一扫

uni-app页面通讯$emit发送数据和$on接受数据之间的小Bug

岛上码农 2021-09-23 阅读 164
uni-app

使用$on接收数据的时候,this指向会出现问题。


index.vue页面发送数据

<input type="text" value="" placeholder="请输入城市名称" @confirm="searchCity"/>

methods: {
            searchCity:function(event){//@confirm命名空间不能有括号,不然报错
                //console.log(event.detail.value)
                uni.navigateBack({
                    delta:1
                })
                uni.$emit('sendCityInfo',{cityName:event.detail.value})
            }
        }

Locationchoose.vue页面接收数据

data() {
            return {
                Location:"城市选择"
            }
},
onLoad() {//$on拿到的数据,this指针会出问题
            let _this = this
            uni.$on('sendCityInfo',function(data){
                console.log(data.cityName)
             // this.Location = data.cityName //视图失效
                _this.Location = data.cityName//必须这样
            })
}
举报

相关推荐

0 条评论