uni-app vue3 微信小程序 编译模式中的启动参数如果有一个是布尔类型,传入进来会全部变成true 但是再路由栈是又是正常的

uni-app vue3 微信小程序 编译模式中的启动参数如果有一个是布尔类型,传入进来会全部变成true 但是再路由栈是又是正常的

示例代码:

const props = defineProps({
index: {
type: Number,
default: 0
},
isCase: {
type: Boolean,
default: false
}
})  

console.log(props);  

console.log(getCurrentPages().pop());

操作步骤:

const props = defineProps({
index: {
type: Number,
default: 0
},
isCase: {
type: Boolean,
default: false
}
})  

console.log(props);  

console.log(getCurrentPages().pop());

预期结果:

参数值接收正确

实际结果:

参数值接收异常

bug描述:

const props = defineProps({  
    index: {  
        type: Number,  
        default: 0  
    },  
    isCase: {  
        type: Boolean,  
        default: false  
    }  
})

//这是传入的参数index=1&isCase=false  

这是接收到的参数Proxy {index: 1, isCase: true}  会出现isCase全是true    

这个是路由栈getCurrentPages()中获取到的options: {index: "1", isCase: "false"}  
信息类别 信息内容
产品分类 uniapp/小程序/微信
PC开发环境 Windows
PC开发环境版本 Windows 10 专业版 22H2
HBuilderX类型 正式
HBuilderX版本 4.66
第三方开发者工具 1.06.2503300
基础库版本 上到最新,下到2.16.1
项目创建方式 HBuilderX

更多关于uni-app vue3 微信小程序 编译模式中的启动参数如果有一个是布尔类型,传入进来会全部变成true 但是再路由栈是又是正常的的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

你这个编译模式的参数,是在微信小程序开发者工具中设置的吗?

更多关于uni-app vue3 微信小程序 编译模式中的启动参数如果有一个是布尔类型,传入进来会全部变成true 但是再路由栈是又是正常的的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这是一个微信小程序平台在编译模式下处理布尔类型参数的已知问题。当通过URL参数传递布尔值时,微信小程序底层会将所有非空字符串转换为true

从你提供的日志可以看出:

  • 路由栈中的options: {index: "1", isCase: "false"}显示参数正确传递为字符串"false"
  • 但在defineProps中接收时,布尔类型的isCase被错误地转换为true

解决方案:

  1. 临时处理方案:手动转换布尔值
const props = defineProps({
  index: {
    type: Number,
    default: 0
  },
  isCase: {
    type: String, // 改为String类型接收
    default: "false"
  }
})

// 使用时手动转换
const isCaseBool = props.isCase === "true"
  1. 推荐方案:使用onLoad生命周期处理参数
onLoad(options) {
  this.isCase = options.isCase === "true"
  this.index = Number(options.index)
}
回到顶部