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

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

作者: kevision | 来源:发表于2020-08-18 21:35 被阅读0次

    当传递的参数是一个完整的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)
        })
    },
    

    相关文章

      网友评论

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

          本文链接:https://www.haomeiwen.com/subject/dhbfjktx.html