【发布时间】:2021-06-20 05:38:24
【问题描述】:
我正在构建一个应用程序,使用 Django Rest Framework 作为后端,使用 Vue 3 作为前端。在我的项目中使用 Vuex 和 Vue Router,以及 Axios 在 Vuex 操作中使用 API。但是,当从后端获取失败时,我无法弄清楚如何显示 404 页面。我尝试了一些方法,例如将路由器导入 vuex 并使用router.push({name: 'notfound'}) 捕获错误,但没有奏效。您对如何实现这一点有任何想法吗?
路由器/index.js
...
{
path: '/route/:category',
name: 'filteredterms',
component: Home
},
{
path: '/:pathMatch(.*)*',
name: 'notfound',
component: NotFound
}
store/index.js
import router from "../router/index.js"
...
async exampleFunc({commit}){
const response = await axios.get(`http://127.0.0.1:8000/api/exampleEndpoint`)
.then(response => {
commit('exampleMutation', response.data)
})
.catch(error => {
if (error.response.status == 404){
router.push({name: 'notfound'})
}
【问题讨论】:
标签: javascript vue.js axios vuex vue-router