重写Page后,在重写的Page里面点击页面跳转后,setData无效,求助?
发布于 6 年前 作者 guiyingpeng 13210 次浏览 来自 问答

我要达到的目的是:app.js里面监听收到新消息后把未读计数写入到globalData.unreadMessages,然后在重写的init_page.js里面监听到后设置wxml的未读数量(希望能全局所有的page都能监听到),目前遇到的问题是首次进入后,不点击其它页面,发送消息footer.wxml未读计数能正常显示,但是点击跳到其他page后,这个监听在init_page.js里面有得到执行,console.log(“未读消息发送变化:”,unreadMessages);能打印最新的消息变化,但是footer.wxml里面的{{msgNum}}不会及时的同步变化,init_page.js里面的that.setData没有改变page里面的值,请教各位老铁帮忙啦~~~ ,我应该怎么解决。。。

page.wxml

....
<view>
  <import src="../common/foot.wxml" />
  <template is="foot" data="{{...footer}}" />
</view>

foot.wxml

<text wx:if="{{jobNum>0}}" class="weui-badge" style="position: absolute; top: -2px; right: -13px; font-size:9px;">{{jobNum}}</text>

app.js

require('./utils/init_page.js'); 
......
App({
  onLaunch: function(options) {
    ...
    that.globalData.unreadMessages = 10;
  },
/**
   * 监听未读消息
   */
  watchUnreadMessages: function (method) {
    var obj = this.globalData;
    Object.defineProperty(obj, "unreadMessages", {
      configurable: true,
      enumerable: true,
      set: function (value) {
        this._unreadMessages = value;
        method(value);
      },
      get: function () {
        return this._unreadMessages;
      }
    })
  },
  globalData: {
    ...
    _unreadMessages:0,//未读消息计数
  }
})

init_page.js

const oldPage = Page
Page = function (app) {
  let oldOnLoad = app.onLoad;
  let oldOnReady = app.onReady;
  let oldOnShow = app.onShow;
  let _app = getApp();
  app.onLoad = function (options) { // 这里必须使用function, 不可以使用箭头函数, 否则this指向错误
    console.log("扩展onLoad", _app.data.im.sdkAppID, _app.globalData.im_chat_ready, _app.globalData.unreadMessages);
    var that = this;
    let page_model = that.getCurrPageModel();
    that.setData({
      footer:{
        jobNum:0,
        msgNum:0,
        page_model: page_model
      }
    });
    console.log("wx.getStorageSync('uid'):", wx.getStorageSync('uid'))
    if (!wx.getStorageSync('uid')) {
      _app.login();
    }

//监听消息计数
    if (_app.globalData.im_chat_ready){
      that.watchUnreadMessages(_app.globalData.unreadMessages);
    }else{
      _app.watchUnreadMessages(that.watchUnreadMessages);
    }
    oldOnLoad.call(this, options);
  },
  app.onReady = function(options){
    console.log("扩展onReady");
    var that = this;
    
    if (typeof oldOnReady === 'function') {
      oldOnReady.call(this, options);
    }
  },
  app.onShow = function(options){
    var that = this;
    //检查是否需要重新登陆
    if (typeof oldOnShow === 'function') {
      oldOnShow.call(this, options);
    }
  },
  app.watchUnreadMessages = function(unreadMessages){
    var that = this;
    console.log("未读消息发送变化:",unreadMessages);
    that.setData({
      footer: {
        jobNum: that.data.footer.jobNum,
        msgNum: unreadMessages,
        page_model: that.getCurrPageModel()
      }
    });
  }
  return oldPage(app)
}
2 回复

已经解决,是自己忽略了一个小错误。。。

// if (_app.globalData.im_chat_ready){
    //   that.watchUnreadMessages(_app.globalData.unreadMessages);
    // }else{
    //   _app.watchUnreadMessages(that.watchUnreadMessages);
    // }
    _app.watchUnreadMessages(that.watchUnreadMessages);

能否给个代码片段,方便调试

回到顶部