在页面中监听正常,只有在自定义组件中监听会出现问题
小程序组件中监听全局变量,方法中的this指向为undefined这是app.js globalData: { userInfo: null, loginMode: true, }, //app 全局属性监听 watch: function (method) { var obj = this.globalData; Object.defineProperty(obj, "userInfo", { //这里的 data 对应 上面 globalData 中的 data configurable: true, enumerable: true, set: function (value) { //动态赋值,传递对象,为 globalData 中对应变量赋值 this._showPictureDetail = value.showPictureDetail; this._pictureTime = value.pictureTime; this._pictureAddress = value.pictureAddress; method(value); }, get: function () { //获取全局变量值,直接返回全部 return this.globalData; } }) } 这是组件监听 created() { getApp().watch(this.watchBack) }, watchBack: function (value) { console.log(this) // 这里的this打印出来是undefined this.setData({ loginMode: false }) },
2021-06-07解决了吗?我也遇到这种问题了
关于swiper在苹果手机上的bug?[代码]<!--消息页面 -->[代码][代码]<!-- 顶部tab栏,可以切换“消息中心”和“联系人”,默认显示消息中心 -->[代码][代码]<[代码][代码]view[代码] [代码]class[代码][代码]=[代码][代码]'msg-tab swiperTab'[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]view[代码] [代码]class[代码][代码]=[代码][代码]"tab-msg swiper-tab-item {{currentTab==0?'active':''}}"[代码] [代码]data-current[代码][代码]=[代码][代码]"0"[代码] [代码]bindtap[代码][代码]=[代码][代码]"clickTab"[代码][代码]>消息中心</[代码][代码]view[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]view[代码] [代码]class[代码][代码]=[代码][代码]"tab-people swiper-tab-item {{currentTab==1?'active':''}}"[代码] [代码]data-current[代码][代码]=[代码][代码]"1"[代码] [代码]bindtap[代码][代码]=[代码][代码]"clickTab"[代码][代码]>联系人</[代码][代码]view[代码][代码]>[代码][代码]</[代码][代码]view[代码][代码]>[代码] [代码]<!-- 消息中心 -->[代码][代码]<[代码][代码]view[代码] [代码]style[代码][代码]=[代码][代码]"width:100%;height:100%;"[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]swiper[代码] [代码]current[代码][代码]=[代码][代码]"{{currentTab}}"[代码] [代码]bindchange[代码][代码]=[代码][代码]"swiperTab"[代码] [代码]class[代码][代码]=[代码][代码]'swiper-contacts'[代码] [代码]style[代码][代码]=[代码][代码]'height:{{minfixheight}}rpx'[代码][代码]>[代码] [代码] [代码][代码]<[代码][代码]swiper-item[代码] [代码]style[代码][代码]=[代码][代码]"margin-top: 75rpx;"[代码] [代码]item-id[代码][代码]=[代码][代码]'0'[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]view[代码] [代码]style[代码][代码]=[代码][代码]'background-color:#fff;border-top:1rpx solid #ddd;'[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]block[代码] [代码]wx:for[代码][代码]=[代码][代码]"{{msg_list}}"[代码] [代码]wx:key[代码][代码]=[代码][代码]"id"[代码] [代码]wx:for-item[代码][代码]=[代码][代码]"item"[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]view[代码] [代码]class[代码][代码]=[代码][代码]"weui-cell weui-cell_access contact-item"[代码] [代码]hover-class[代码][代码]=[代码][代码]"weui-cell_active"[代码] [代码]style[代码][代码]=[代码][代码]'padding:20rpx 20rpx;'[代码] [代码]bindtap[代码][代码]=[代码][代码]"listTochat"[代码] [代码]data-item[代码][代码]=[代码][代码]"{{item}}"[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]view[代码] [代码]class[代码][代码]=[代码][代码]'contact-msg'[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]view[代码] [代码]style[代码][代码]=[代码][代码]"width:13%; display: flex;flex-direction: row;"[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]image[代码] [代码]src[代码][代码]=[代码][代码]'{{item.friend_picture}}'[代码] [代码]class[代码][代码]=[代码][代码]'contact-icon'[代码] [代码]catchtap[代码][代码]=[代码][代码]'show_information'[代码] [代码]data-item[代码][代码]=[代码][代码]"{{item}}"[代码][代码]></[代码][代码]image[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]view[代码] [代码]wx:if[代码][代码]=[代码][代码]'{{item.msg_arr[item.msg_arr.length-1].unread == 0}}'[代码] [代码]style[代码][代码]=[代码][代码]"background-color:red;height:16rpx;width:16rpx;border-radius:16rpx;color:red;z-index:99;margin-left:-16rpx;"[代码][代码]></[代码][代码]view[代码][代码]>[代码][代码] [代码][代码]</[代码][代码]view[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]view[代码] [代码]style[代码][代码]=[代码][代码]'display:flex;flex-direction: column;padding-left:6rpx;width:87%;'[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]view[代码] [代码]style[代码][代码]=[代码][代码]'display:flex;flex-direction: row;'[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]text[代码] [代码]class[代码][代码]=[代码][代码]'contact-name'[代码][代码]>{{item.friend_name}}</[代码][代码]text[代码][代码]>[代码][代码] [代码][代码]</[代码][代码]view[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]text[代码] [代码]class[代码][代码]=[代码][代码]'contact-phone'[代码][代码]>{{item.msg_arr[item.msg_arr.length-1].content?item.msg_arr[item.msg_arr.length-1].content:item.msg_arr[item.msg_arr.length-1].Content}}</[代码][代码]text[代码][代码]>[代码][代码] [代码][代码]</[代码][代码]view[代码][代码]>[代码][代码] [代码][代码]</[代码][代码]view[代码][代码]>[代码][代码] [代码][代码]<!-- <view class='iconfont icon-dianhua phone-icon' catchtap="calling" data-phone_num='{{user.phone}}'></view> -->[代码][代码] [代码][代码]</[代码][代码]view[代码][代码]>[代码][代码] [代码][代码]</[代码][代码]block[代码][代码]>[代码][代码] [代码][代码]</[代码][代码]view[代码][代码]>[代码][代码] [代码][代码]</[代码][代码]swiper-item[代码][代码]>[代码] [代码] [代码][代码]<!-- 联系人 -->[代码][代码] [代码][代码]<[代码][代码]swiper-item[代码] [代码]style[代码][代码]=[代码][代码]"margin-top: 75rpx;"[代码] [代码]item-id[代码][代码]=[代码][代码]'1'[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]view[代码] [代码]class[代码][代码]=[代码][代码]'contacts'[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]view[代码] [代码]class[代码][代码]=[代码][代码]"weui_cells weui-cells_after-title"[代码][代码]>[代码] [代码] [代码][代码]<[代码][代码]block[代码] [代码]wx:for[代码][代码]=[代码][代码]"{{friends}}"[代码] [代码]wx:key[代码][代码]=[代码][代码]"groupId"[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]view[代码] [代码]class[代码][代码]=[代码][代码]"weui-cell weui-cell_access department"[代码] [代码]hover-class[代码][代码]=[代码][代码]"weui-cell_active"[代码] [代码]bindtap[代码][代码]=[代码][代码]'spread'[代码] [代码]data-spread[代码][代码]=[代码][代码]'{{index}}'[代码] [代码]data-friends[代码][代码]=[代码][代码]'{{friends}}'[代码] [代码]data-minfixheight[代码][代码]=[代码][代码]'{{minfixheight}}'[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]view[代码] [代码]class[代码][代码]=[代码][代码]'president-icon iconfont {{item.icon}}'[代码][代码]></[代码][代码]view[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]view[代码] [代码]class[代码][代码]=[代码][代码]"weui-cell__bd todo-task"[代码][代码]>{{item.name}}[代码][代码] [代码][代码]<[代码][代码]text[代码] [代码]class[代码][代码]=[代码][代码]'num'[代码][代码]>({{item.list.length}})</[代码][代码]text[代码][代码]>[代码][代码] [代码][代码]</[代码][代码]view[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]view[代码] [代码]class[代码][代码]=[代码][代码]'unfold'[代码] [代码]style[代码][代码]=[代码][代码]'transform: rotate({{item.isspread?"135deg":"45deg"}});'[代码][代码]></[代码][代码]view[代码][代码]>[代码][代码] [代码][代码]</[代码][代码]view[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]view[代码] [代码]class[代码][代码]=[代码][代码]"weui-cell weui-cell_access groups-contents0 {{item.isspread&&item.list.length!=0?'open':'close'}}"[代码][代码]>[代码] [代码] [代码][代码]<[代码][代码]block[代码] [代码]wx:for[代码][代码]=[代码][代码]"{{item.list}}"[代码] [代码]wx:key[代码][代码]=[代码][代码]"id"[代码] [代码]wx:for-item[代码][代码]=[代码][代码]"user"[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]view[代码] [代码]class[代码][代码]=[代码][代码]"weui-cell weui-cell_access contact-item"[代码] [代码]hover-class[代码][代码]=[代码][代码]"weui-cell_active"[代码] [代码]style[代码][代码]=[代码][代码]'padding:0px 20rpx;margin-left:20rpx;'[代码] [代码]bindtap[代码][代码]=[代码][代码]"contactTochat"[代码] [代码]data-item[代码][代码]=[代码][代码]"{{user}}"[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]view[代码] [代码]class[代码][代码]=[代码][代码]'contact-person'[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]image[代码] [代码]src[代码][代码]=[代码][代码]'{{user.picture}}'[代码] [代码]class[代码][代码]=[代码][代码]'contact-icon'[代码] [代码]catchtap[代码][代码]=[代码][代码]'show_information'[代码] [代码]data-item[代码][代码]=[代码][代码]"{{user}}"[代码][代码]></[代码][代码]image[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]view[代码] [代码]style[代码][代码]=[代码][代码]'display:flex;flex-direction: column;margin-left:20rpx;'[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]view[代码] [代码]style[代码][代码]=[代码][代码]'display:flex;flex-direction: row;'[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]text[代码] [代码]class[代码][代码]=[代码][代码]'contact-name'[代码][代码]>{{user.name}}</[代码][代码]text[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]text[代码] [代码]style[代码][代码]=[代码][代码]'font-size: 28rpx;color: #888;line-height:54rpx;'[代码][代码]> ({{user.job_name}})</[代码][代码]text[代码][代码]>[代码][代码] [代码][代码]</[代码][代码]view[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]text[代码] [代码]class[代码][代码]=[代码][代码]'contact-phone'[代码][代码]>{{user.phone}}</[代码][代码]text[代码][代码]>[代码][代码] [代码][代码]</[代码][代码]view[代码][代码]>[代码][代码] [代码][代码]</[代码][代码]view[代码][代码]>[代码][代码] [代码][代码]<[代码][代码]view[代码] [代码]class[代码][代码]=[代码][代码]'iconfont icon-dianhua phone-icon'[代码] [代码]catchtap[代码][代码]=[代码][代码]"calling"[代码] [代码]data-phone_num[代码][代码]=[代码][代码]'{{user.phone}}'[代码][代码]></[代码][代码]view[代码][代码]>[代码][代码] [代码][代码]</[代码][代码]view[代码][代码]>[代码][代码] [代码][代码]</[代码][代码]block[代码][代码]>[代码][代码] [代码][代码]</[代码][代码]view[代码][代码]>[代码][代码] [代码][代码]</[代码][代码]block[代码][代码]>[代码] [代码] [代码][代码]</[代码][代码]view[代码][代码]>[代码][代码] [代码][代码]</[代码][代码]view[代码][代码]>[代码][代码] [代码][代码]</[代码][代码]swiper-item[代码][代码]>[代码][代码] [代码][代码]</[代码][代码]swiper[代码][代码]>[代码][代码]</[代码][代码]view[代码][代码]>[代码][代码]//滑动切换[代码][代码] [代码][代码]swiperTab: function(e) {[代码][代码] [代码][代码]var that = this;[代码][代码] [代码][代码]that.setData({[代码][代码] [代码][代码]currentTab: e.detail.current[代码][代码] [代码][代码]});[代码][代码] [代码][代码]if (this.noswiper) {[代码][代码] [代码][代码]if (e.detail.current == 1) {[代码][代码] [代码][代码]this.getFriends();[代码] [代码] [代码][代码]} else {[代码][代码] [代码][代码]this.getConversations();[代码][代码] [代码][代码]}[代码][代码] [代码][代码]}[代码][代码] [代码][代码]this.noswiper = true;[代码] [代码] [代码][代码]},[代码] [代码] [代码][代码]//点击切换[代码][代码] [代码][代码]clickTab: function(e) {[代码][代码] [代码][代码]var that = this;[代码][代码] [代码][代码]this.noswiper = false;[代码][代码] [代码][代码]if (this.data.currentTab == e.target.dataset.current) { //点击的是当前选项卡,则点击不生效[代码][代码] [代码][代码]return false;[代码][代码] [代码][代码]} else { //点击其他选项卡,切换显示[代码][代码] [代码][代码]that.setData({[代码][代码] [代码][代码]currentTab: e.target.dataset.current[代码][代码] [代码][代码]});[代码] [代码] [代码][代码]if (e.target.dataset.current == 1) {[代码][代码] [代码][代码]this.getFriends();[代码][代码] [代码][代码]} else {[代码][代码] [代码][代码]this.getConversations();[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]}[代码][代码] [代码][代码]},[代码][图片][图片] 这是开发者工具中的效果,也是安卓手机的效果,也是预想中的效果,但是在苹果手机中右边swiper-item的列表直接拼接在左边swiper-item下面了[图片]
2020-12-09