uniapp アプレットはプラグインuni-simple-router を使用します。
この時点で、存在しないルートにアクセスすると、エラーが表示されます: /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 页面的路径
});
},