anu icon indicating copy to clipboard operation
anu copied to clipboard

微信小程序自定义转发onShareAppMessage只调用一次

Open lyf7 opened this issue 7 years ago • 4 comments

image 上面的代码可能有问题,当hook等于onShareAppMessage时,hook重新赋值为onShare,同一页面再次转发时,上面的 fn = instance[hook]等于undefined,因为instance只有onShareAppMessage方法,没有onShare方法。

lyf7 avatar May 21 '19 10:05 lyf7

我们测试过,没有问题啊。 config是微信小程序的配置对象。它每次都会找到对应的react实例,又不会删掉react实例的方法。

RubyLouvre avatar May 23 '19 09:05 RubyLouvre

上面的代码闭包的hook被改了,需要页面有onShareAppMessage和onShare两个方法,第二次转发才正常。

lyf7 avatar May 23 '19 11:05 lyf7

 Array(
        'onPageScroll',
        'onShareAppMessage',
        'onReachBottom',
        'onPullDownRefresh',
        'onResize',
        'onShow',
        'onHide'
    ).forEach(function(hook) {
        config[hook] = function(e) {
            let instance = this.reactInstance,
             pageHook = hook,
             param = e 
            if (pageHook === 'onShareAppMessage'){
                if( !instance.onShare){
                    instance.onShare = instance.onShareAppMessage
                }
                pageHook = 'onShare';
            } else if (pageHook === 'onShow'){
                if(this.options){ //支付宝小程序不存在this.options
                   instance.props.query = this.options ;
                }
                param = instance.props.query
                //在百度小程序,从A页面跳转到B页面,模拟器下是先触发A的onHide再触发B的onShow
                //真机下,却是先触发B的onShow再触发A的onHide,其他小程序可能也有这问题,因此我们只在onShow
                //里修改全局对象的属性
                _getApp().$$page = this;
                _getApp().$$pagePath = instance.props.path;
            }
            let methods = [pageHook,  appHooks[pageHook] ];
            for(let i = 0; i < 2; i ++){
                let method = methods[i];
                let host = i ?  _getApp(): instance;
                if( method && host && isFn(host[method]) ){
                   let ret = host[method](param);
                   if(ret !== void 0){
                       return ret;
                   }
                }
            }
        };
    });

RubyLouvre avatar May 23 '19 11:05 RubyLouvre

不好意思,我更新一下。

lyf7 avatar May 23 '19 11:05 lyf7