uniapp小程序使用了插件uni-simple-router
这时候如果访问不存在的路由 会提示Error: /pages/error/* 路径无法在路由表中找到!检查跳转路径及路由表
使用插件uni-simple-router,导致不存在页面跳转404页面问题解决
解决方案
-插件携带方法
// page.json
{
"path": "pages/error/404",
"name": "404", // 添加404页面name属性
"style": {
"app-plus": {
"titleNView": false
}
}
}
//router.js
const router = createRouter({
platform: process.env.VUE_APP_PLATFORM,
routes: [
...ROUTES,
{
path: '*',
redirect:(to)=>{
return {
name:'404'} // 匹配非定义的路由地址
}
},
],
// 自带得方法被插件整没了,只能在报错地方退出咯
routerErrorEach: (error, router) => {
console.log('warning');
// 可自行修改退出类型
// plus.nativeUI.toast = (function(str) {
// if (str == '再次返回退出应用') {
// plus.runtime.quit();
// } else {
// uni.showToast({
// title: '再次返回退出应用',
// icon: 'none'
// })
// }
// });
if (uni.getSystemInfoSync().platform == 'ios') {
plus.ios.import("UIApplication").sharedApplication().performSelector("exit")
} else if (uni.getSystemInfoSync().platform == 'android') {
plus.runtime.quit();
}
}
});
- 使用app.vue自带得属性跳转
// page.json
{
"path": "pages/error/404",
// "name": "404", 可以不需要携带name属性
"style": {
"app-plus": {
"titleNView": false
}
}
}
//router.js
const router = createRouter({
platform: process.env.VUE_APP_PLATFORM,
routes: [
...ROUTES,
{
path: '*'
// 不需要配置跳转路由
// redirect:(to)=>{
// return {name:'404'}
//}
},
]
});
// app.vue页面自带方法
onPageNotFound: function() {
uni.navigateTo({
url: "/pages/error/404", // 404 页面的路径
});
},