Plug-in uni-simple-router, error Error: /pages/error/* path cannot be found in the routing table, check the jump path and routing table

The uniapp applet uses the plug-in uni-simple-router.
At this time, if you access a route that does not exist, it will prompt Error: /pages/error/* The path cannot be found in the routing table! Check the jump path and routing table.
Use the plug-in uni-simple-router to solve the problem of non-existent page jump 404 page.
Solution

-How to carry plug-ins

// 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();
		}
	}  
});
  • Use app.vue’s own attributes to jump
// 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 页面的路径
	});
},

Guess you like

Origin blog.csdn.net/l2345432l/article/details/122898047