小程序在路由传参接收时获取不全(个人笔记)

当传递的参数是一个完整的url时,如:http://www.baidu.com?id=1&name=ken

wx.navigateTo({
    url: '/pages/preview/preview?file=' +url,
    success: (result) => {
        console.log("result", result);
    },
    fail: () => {},
        complete: () => {}
  });

接收参数:只有http://www.baidu.com部分

    onLoad: function(options) {
        console.log("options", options.file); // 只有http://www.baidu.com部分
        this.setData({
            file: options.file
        })
    },

解决方法:encodeURIComponent() 编码

定义和用法:
encodeURIComponent() 函数可把字符串作为 URI 组件进行编码。
该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。

wx.navigateTo({
      url: '/pages/preview/preview?file=' + encodeURIComponent(url),
      success: (result) => {
      console.log("result", result);
     },
      fail: () => {},
      complete: () => {}
    });
    onLoad: function(options) {
        console.log("options", decodeURIComponent(options.file)); // http://www.baidu.com?id=1&name=ken
    this.setData({
        file: decodeURIComponent(options.file)
    })
},

你可能感兴趣的:(小程序在路由传参接收时获取不全(个人笔记))