加载状态组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| export default function LazyLoadView(AsyncView, needLoading = true) { const AsyncHandler = () => ({ component: AsyncView, loading: needLoading ? require('./LazyLoadLoading').default : null, error: needLoading ? require('./LazyLoadError').default : null })
return Promise.resolve({ functional: true, render(h, { data, children }) { return h(AsyncHandler, data, children) } }) }
export default { functional: true, render(h, context) { return <div>loading</div> } }
export default { functional: true, render(h, context) { return <div>error</div> } }
|
启用加载状态组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
import Vue from 'vue' import VueRouter from 'vue-router' import LazyLoadView from '/xx dir/LazyLoadView'
Vue.use(VueRouter)
const routes = [ { path: '/home', name: 'Home', component: () => LazyLoadView(import( '@/views/Home')), meta: { title: 'Home' } },
{ path: '/about', name: 'About', component: () => LazyLoadView(import( '@/views/About')), meta: { title: 'About' } },
{ path: '*', redirect: '/404', hidden: true } ]
|
LazyLoadView 导致组件内导航守卫失效问题解决方案
需要注意的是使用 LazyLoadView
会导致组件内导航守卫失效,这是我的一种尝试方案