uni-app中uni.reLaunch的url参数使用变量时报错
uni-app中uni.reLaunch的url参数使用变量时报错
示例代码:
//1.报错情况
//分包地址
let str = "/epermarketSub/NewType?ProductCataID=604"
uni.reLaunch({
url:str,
fail(err) {
console.log(err);
//当前页面在/pages/Authorization/目录下此时报错信息如下
//errMsg: "reLaunch:fail page /pages/Authorization/ /epermarketSub/NewType?ProductCataID=604 is not found"
//查找了主包的位置后面又拼接了分包的地址 所以报错 但是为什么会查找主包呢 我给的url是分包地址啊
}
});
//2.正常可跳转情况
uni.reLaunch({
url:"/epermarketSub/NewType?ProductCataID=604" //或使用“../../epermarketSub/NewType?ProductCataID=604”也可正常跳转,
fail(err) {
console.log(err);
}
});
操作步骤:
A页面使用uni.reLaunch 将url替换成变量时 报错未找到B页面 携带了A页面的前置路径/pages/Authorization
预期结果:
预期应该直接跳转到B页面 不应该会出现错误
实际结果:
出现错误 并且是查找了主包的路径
bug描述:
前置条件:
- 分包完整页面路径“/epermarketSub/NewType?ProductCataID=604” 简称页面B
- 主包当前页面完整路径“pages/Authorization/citySelect” 简称页面A
- 两者均已正常在pages.json中配置主包和分包目录中配置
完整报错信息如下:
errMsg: "reLaunch:fail page /pages/Authorization/ /epermarketSub/NewType?ProductCataID=604 is not found"
开发环境 | 版本号 | 项目创建方式 |
---|---|---|
Windows | win10 | HBuilderX |
更多关于uni-app中uni.reLaunch的url参数使用变量时报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html
您好,我试了一下未能复现,请提供一个能够复现的demo工程
更多关于uni-app中uni.reLaunch的url参数使用变量时报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html
在 uni-app
中使用 uni.reLaunch
时,如果你在 url
参数中使用了变量,可能会遇到报错的情况。这通常是因为 url
参数的类型或格式不正确,或者变量没有正确传递。
常见问题及解决方法
-
确保变量是字符串类型
uni.reLaunch
的url
参数需要是一个字符串。如果你传递了一个非字符串类型的变量(如对象、数组等),就会报错。// 正确示例 let pagePath = '/pages/index/index'; uni.reLaunch({ url: pagePath }); // 错误示例 let pagePath = { path: '/pages/index/index' }; uni.reLaunch({ url: pagePath // 这里会报错,因为 pagePath 是对象 });
-
拼接路径时注意格式
如果你需要动态拼接路径,确保路径的格式正确,尤其是斜杠 (/
) 的使用。let pageName = 'index'; uni.reLaunch({ url: `/pages/${pageName}/${pageName}` // 正确拼接路径 });
-
传递参数时使用
encodeURIComponent
如果url
中包含查询参数,确保对参数进行encodeURIComponent
编码,以避免特殊字符导致的错误。let queryParam = encodeURIComponent('some value'); uni.reLaunch({ url: `/pages/index/index?param=${queryParam}` });
-
检查变量是否已定义
确保你使用的变量在调用uni.reLaunch
之前已经正确定义和赋值。let pagePath; // 假设这里有一些逻辑来赋值 pagePath pagePath = '/pages/index/index'; uni.reLaunch({ url: pagePath // 确保 pagePath 已经被赋值 });
-
使用模板字符串
如果你需要动态生成路径,可以使用模板字符串来简化代码。let pageName = 'index'; uni.reLaunch({ url: `/pages/${pageName}/${pageName}` });
示例代码
// 正确使用示例
let pageName = 'index';
let queryParam = encodeURIComponent('some value');
let fullPath = `/pages/${pageName}/${pageName}?param=${queryParam}`;
uni.reLaunch({
url: fullPath
});