getCurrentPages()在uni-app中获取query参数如果参数是超长数字会出现将其转换为科学计数法的问题

getCurrentPages()在uni-app中获取query参数如果参数是超长数字会出现将其转换为科学计数法的问题

示例代码:

如现有一个url:https://example.com/detail?id=1234567890123456789012345

const pages = getCurrentPages()  
const current = pages[pages.length - 1]  
const { options } = current  
console.log(options.id)  // 此处将会把id变成一个以科学计数法记录的数字

操作步骤:

如现有一个url:https://example.com/detail?id=1234567890123456789012345

// 在onLoad()中插入以下代码  
const pages = getCurrentPages()  
const current = pages[pages.length - 1]  
const { options } = current  
console.log(options.id)

预期结果:

log应该打印1234567890123456789012345

实际结果:

log输出的结果是1.2345678901234568e+24

bug描述:

getCurrentPages()获取query参数,如果参数是超长数字,会出现将其转换为科学计数法的问题

bug1

bug2

信息项 内容
产品分类 uniapp/H5
PC开发环境 Windows
操作系统版本 19045.6456
HBuilderX类型 正式
HBuilderX版本 3.2.16
浏览器平台 微信内置浏览器
浏览器版本 1.06.2401020
项目创建方式 HBuilderX

更多关于getCurrentPages()在uni-app中获取query参数如果参数是超长数字会出现将其转换为科学计数法的问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

你弄成字符串的试试

更多关于getCurrentPages()在uni-app中获取query参数如果参数是超长数字会出现将其转换为科学计数法的问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在uni-app中,getCurrentPages()获取的query参数确实存在长数字被自动转换为科学计数法的问题。这是因为JavaScript在处理超过安全整数范围(±2^53-1)的数字时会自动转换为科学计数法表示。

解决方案:

  1. 在URL传参时添加引号

    // 将参数用引号包裹
    https://example.com/detail?id="1234567890123456789012345"
    
  2. 在接收参数时转换为字符串

    const pages = getCurrentPages()  
    const current = pages[pages.length - 1]  
    const { options } = current
    // 强制转换为字符串
    const id = String(options.id)
    console.log(id)  // 输出:1234567890123456789012345
    
  3. 使用URLSearchParams获取原始参数

    const urlParams = new URLSearchParams(window.location.search)
    const id = urlParams.get('id')
    console.log(id)  // 输出原始字符串值
    
  4. 在onLoad生命周期中直接获取

    onLoad(query) {
      console.log(query.id)  // 这里获取的是原始字符串
    }
回到顶部