HarmonyOS鸿蒙Next中路由带参数跳转页面的问题
HarmonyOS鸿蒙Next中路由带参数跳转页面的问题
咨询描述
我在第一个页面用路由传递参数
router.pushUrl({
url: 'pages/CPCL',
params: this.baseConnect
}).then(() => {
console.info('Succeeded in jumping to the CPCL page.')
}).catch((err: BusinessError) => {
console.error(`Failed to jump to the CPCL page. Code is ${err.code}, message is ${err.message}`)
})
第二个页面接收数据
onPageShow(): void {
const params = router.getParams() as BaseConnection; // 获取传递过来的参数对象
if (params) {
this.baseConnect = params
this.helper = new PrinterCPCLHelper(this.baseConnect)
}
}
然后我执行这个函数时报错了
if (!this.baseConnect.isConnect()) {
promptAction.showToast({ message: "请先连接打印机", duration: 2000 })
this.message = "请先连接打印机"
return
}
报的错误是
[main_thread.cpp:1589]
02-27 15:14:06.508 60695-60695 C01317/com.hy.common/AppKit com.hy.common E com.hy.common is about to exit due to RuntimeError
02-27 15:14:06.508 60695-60695 C01317/com.hy.common/AppKit com.hy.common E Error type: TypeError
02-27 15:14:06.508 60695-60695 C01317/com.hy.common/AppKit com.hy.common E Error name: TypeError
02-27 15:14:06.508 60695-60695 C01317/com.hy.common/AppKit com.hy.common E Error message: is not callable
02-27 15:14:06.508 60695-60695 C01317/com.hy.common/AppKit com.hy.common E SourceCode:
02-27 15:14:06.508 60695-60695 C01317/com.hy.common/AppKit com.hy.common E if (!this.baseConnect.isConnect()) {
02-27 15:14:06.508 60695-60695 C01317/com.hy.common/AppKit com.hy.common E ^
更多关于HarmonyOS鸿蒙Next中路由带参数跳转页面的问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html
如果参数没有传过来,就会导致异常crash,目前可以判断的是 this.baseConnect
可能是不存在 导致读取 isConnect()
应用crash了,router传参不能传递方法
https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-router#routeroptions
更多关于HarmonyOS鸿蒙Next中路由带参数跳转页面的问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
router无法传递map类型的数据,param中只能包含基础类型的数据,router传参请参考:zh-cn/application-dev/reference/apis-arkui/js-apis-router.md · OpenHarmony/docs - 码云 - 开源中国,推荐使用Navigation进行复杂数据类型的传参。
参考链接:[https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs-V5/faqs-arkui-261-V5]
在HarmonyOS鸿蒙Next中,路由带参数跳转页面可以通过router.push
方法实现。参数可以附加在url
中,例如:router.push({ url: 'pages/PageName', params: { key: 'value' } })
。目标页面通过router.getParams
获取传递的参数。确保目标页面在pages
目录下,并在config.json
中正确配置路由。