小程序通過 url 向內嵌 H5 傳參注意事項


當在小程序中通過 url 向 <web-view> 內嵌的 H5 傳參時,當包含特殊字符時需要進行編碼處理(不然 <web-view> 中是拿不到值的,小程序竟然沒有錯誤提示...):

1、test.wxml

<view>
  <web-view src="{{url}}"></web-view>
</view>

2、test.js,對參數進行編碼處理:

Page({

    /**
     * 頁面的初始數據
     */
    data: {
        url: 'https://xxx.xx.com'
    },

    /**
     * 生命周期函數--監聽頁面加載
     */
    onLoad: function(option) {
        let loginName = encodeURI(wx.getStorageSync('loginName'));

        try {
            //相關操作
            }
        } catch (e) {
            wx.navigateTo({
                url: '../login/login'
            })
        }

        let pageUrl = encodeURI(option.url);
        this.setData({
            url: `${this.data.url}${pageUrl}loginName=${loginName}`
        });

    },
    onReady: function() {

    },
    onShow: function(option) {
        
    }

})

3、H5 端獲取參數時需要進行解碼處理。

//獲取地址欄參數
    getQueryString: (name) => {
        let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        let r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return unescape(decodeURI(r[2]));
            return null;
        }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM