Micro-channel applet for phone numbers stored in the module will pop up inside the parameter variable emptied onshow

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/qq_34672907/article/details/90636954

Yesterday was doing when demand found small micro-channel program will also variable assignment inside the applet onshow Click for phone number when empty.

Check yourself a little pop-up for phone numbers when the micro-channel applet also called onhide and onshow, that is equivalent to default out of the current page, he jumped out of the current page that would certainly return again called onshow friends. Find out the reason certainly is doing it.

1. Set a variable onhide when used to determine this variable is not a variable inside onshow should not emptied at onhide.

2.onhide when you do not empty, but another point is, if I set the variable change in bindgetphonenumber function inside, onhide or when the variable cleared, indicating onhide executed earlier than bindgetphonenumber, we can only write a catchtap need to monitor their own function inside the onhide value empty.

On the following codes:

html settings:

<button class='get_phone_number ' catchtap='getTip' open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"></button>

js implementation, the variable I have here is cabinet_id, as follows:

Page({
    data: {
        cabinet_id:null,//柜子的id
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow:function(){
        console.log("我是onshow")
        this.setData({cabinet_id:123435})      
    },
    getTip(){
        console.log("点击获取电话的时候的id"+this.data.cabinet_id)
        wx.setStorageSync("popToAllow", true);
    },
    //得到手机号
    getPhoneNumber(e) {
        // 得到手机号给后台
        wx.removeStorageSync("popToAllow");//获取了手机号之后记得把popToAllow清空防止值存在影响之后逻辑
    },
    
    onHide:function(){
        console.log("我是隐藏的页面")
        if (wx.getStorageSync("popToAllow")){//popToAllow为true需要保留cabinet_id
            console.log("我是本应该保留id" + this.data.cabinet_id)
        }else{
            this.setData({ cabinet_id: null }); //将柜子id清空掉
        }

    },
})

I was way to the existence of the cache, you can use other methods, such as direct variable popToAllow OK, because at the time onhide variables still exist, after onhide finished popToAllow will be removed, so to achieve the same effect.

Notes do here.

Guess you like

Origin blog.csdn.net/qq_34672907/article/details/90636954