由于 Nuxt 的 fetch hook 无法并行运行,因此我需要一种方法来取消导航到其他路由时在 fetch hook 中完成的请求,以便用户在导航到其他路由的主页时不必等待第一次 fetch 完成。所以我找到了这种方法:How to cancel all Axios requests on Route Change
所以我为 Next 创建了这些插件文件:
路由器.js
export default ({ app, store }) => {
// Every time the route changes (fired on initialization too)
app.router.beforeEach((to, from, next) => {
store.dispatch('cancel/cancel_pending_requests')
next()
})
}
Run Code Online (Sandbox Code Playgroud)
axios.js
export default function ({ $axios, redirect, store }) {
$axios.onRequest((config) => {
const source = $axios.CancelToken.source()
config.cancelToken = source.token
store.commit('cancel/ADD_CANCEL_TOKEN', source)
return config
}, function (error) {
return …Run Code Online (Sandbox Code Playgroud)