Requirement: when responding to an error, the rendering will display the interface with error information
preparation: create error information interfaces 400. Vue and 500. Vue in content
note: pay attention to the level of routing where the error interface needs to be placed (I’m under children)
1、 Create route (routes/index. JS)
export default {
mode: 'history',
routes: [
// Dynamic path parameters Start with a colon
{
name:'index',
path: '/',
component: () => import('@/components/QBLayout/index.vue'),
children: [
{
name:'index',
path: '/',
component: () => import('@/components/Index/index.vue'),
},
{
name: 'error_403',
path: '403',
component:()=>import('@/components/errorStatus/403.vue')
},
{
name: 'error_500',
path: '500',
component:()=>import('@/components/errorStatus/500.vue')
},
{
name: 'error_404',//Note that this 404 route should be placed at the end
path: '*',
component:()=>import('@/components/errorStatus/404.vue')
}
]
}
]
}
2、 Response interception (util/HTTP. JS)
import Axios from 'axios'
//1.Wrapping method: for exception response codes are handled separately
const codeErrorHandle = (resData)=>{
switch (resData.code) {
case 404:
router.push({
name:'error_404'
})
break;
case 500:
router.push({
name:'error_500'
})
break;
default:
}
//2, axios interception
const instance = Axios.create({ timeout: 25000 });
//3, response interception: mainly for response processing as follows
instance.interceptors.response.use(
res => {
if (res.status && +res.status < 300 && res.data && +res.data.code === 1)
return Promise.resolve(res.data.data);
} else {//Call codeErrorHandle to handle exception response codes
codeErrorHandle(res.data);
return Promise.reject(res);
}
},
error => {//This is other response state judgement, not this topic, so omit it here for now}
);
Effect interface
http://localhost : 8080/test. (enter the interface not in the route, for example: test, and then press enter to display the 404 interface)
Read More:
- Vue2.0: How to Use vue3 api to encapsulate Axios
- [Solved] Vue item packaging error: Failed to load resource: the server responded with a status of 404 (Not Found)
- Solve the problem of repeatedly clicking the same route console in Vue to report an error
- [Solved] vue3vite Error: Failed to resolve import “@vue/server-renderer from “src\App.vue“. Does the file exist
- Vue: How to Solve error avoided redundant navigation to current location: “/xxx”
- How to Solve “Vue is not defined” Error
- [Solved] Vue route jumps to the same page many times error: Navigationduplicated
- [Solved] Vue console error: navigationduplicated: avoided redundant navigation to current location
- Vue Error: Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location
- [Solved] Vue Error: Avoided redundant navigation to current location:/xxxx
- Vue warn]: vue3 element Component emit Pass Event Error
- Vue elementui: solution for error: avoided redundant navigation to current location: “/xxx”
- How to Solve Vue route jump repeated clicks Error
- Full screen scrolling by Vue + Vue awesomeswiper
- [Solved] Vue3.0 Error: The component has been registered but not used vue/no-unused-components, Close eslint
- [Solved] Vue Error: Uncaught TypeError: Vue.createApp is not a function
- [Solved] Vue Route Error: Uncaught TypeError: (0 , vue__WEBPACK_IMPORTED_MODULE_42__.defineComponent) is not a function
- How to Solve Vue3 Import lodash error
- How to open a page in a new window by Vue router
- [Solved] Vue3 Configuration routing error: Catch all routes (“*“) must now be defined using a param with a custom regexp.