学习vue elementui最大的挑战就是执行逻辑的理清,vue中存在很多隐式的调用,这让我们的思维非常的混乱和跳跃,像router的捕获以及到底什么时候去调用了一个vuex的方法,都非常的隐晦,造成我们不能顺着一条线,顺藤摸瓜式的理解整个事物,这也是我们拿到一个vue的项目一头雾水的根源。本文抽丝剥茧,力图将vue的执行流程做一个梳理。能让我们对整个执行流程有一个全面清晰的认识:
我这里以el-admin的前端代码为例来讲解,代码下载请访问https://gitee.com/elunez/eladmin-web:
所有的程序都有一个开始,这就像是开机一样,vue的开始就是main.js
1.main.js
new Vue({
el: '#app',
router,
store,
render: h => h(App)
})
可以看到,这里加载了router路由和store(数据)
但是这个仅仅是程序的开始,我们作为一个使用者,首先去理解这个程序的执行却是从登录开始的。我们想知道的是登录页面发生了什么。
当我们访问http://localhost:8013/#/login的时候到底是谁负责带路找到页面呢?答案是路由
2.路由,router/index.js
router下面有两个文件,一个是index.js 一个是router.js
由于我们要对所有的请求进行安全检查,所以我们需要在请求到达页面之前,设置一个过滤器,这个过滤的目的是,比如,当用户没有登陆的时候直接重定向到登录页,当用户登录的时候去拉取菜单。index.js里面定义了拦截器
router.beforeEach((to, from, next) => {
if (to.meta.title) {
document.title = to.meta.title + ' - ' + Config.title
}
NProgress.start()
if (getToken()) {
// 已登录且要跳转的页面是登录页
if (to.path === '/login') {
next({path: '/'})
NProgress.done()
} else {
if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
store.dispatch('GetInfo').then(res => { // 拉取user_info
console.log("GetInfo");
// 动态路由,拉取菜单
loadMenus(next, to)
}).catch((err) => {
console.log(err)
store.dispatch('LogOut').then(() => {
location.reload() // 为了重新实例化vue-router对象 避免bug
})
})
// 登录时未拉取 菜单,在此处拉取
} else if (store.getters.loadMenus) {
// 修改成false,防止死循环
store.dispatch('updateLoadMenus').then(res => {
})
loadMenus(next, to)
} else {
next()
}
}
} else {
/* has no token*/
if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
next()
} else {
next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页
NProgress.done()
}
}
})
这个就是一个拦截器,在路由到真正的页面之前,进行数据准备和登录状态验证。
可以看到当我们未登录访问任意页面的时候,index.js没有获取到token于是直接重定向到了login这个url
而login具体的指向的页面在route.js里面定义
3.route.js
import Vue from 'vue'
import Router from 'vue-router'
import Layout from '../layout/index'
Vue.use(Router)
export const constantRouterMap = [
{
path: '/login',
meta: {title: '登录', noCache: true},
component: (resolve) => require(['@/views/login'], resolve),
hidden: true
},
{
path: '/404',
component: (resolve) => require(['@/views/features/404'], resolve),
hidden: true
},
{
path: '/401',
component: (resolve) => require(['@/views/features/401'], resolve),
hidden: true
},
{
path: '/redirect',
component: Layout,
hidden: true,
children: [
{
path: '/redirect/:path*',
component: (resolve) => require(['@/views/features/redirect'], resolve)
}
]
},
{
path: '/',
component: Layout,
redirect: '/dashboard',
children: [
{
path: 'dashboard',
component: (resolve) => require(['@/views/home'], resolve),
name: 'Dashboard',
meta: {title: '首页', icon: 'index', affix: true, noCache: true}
}
]
},
{
path: '/user',
component: Layout,
hidden: true,
redirect: 'noredirect',
children: [
{
path: 'center',
component: (resolve) => require(['@/views/system/user/center'], resolve),
name: '个人中心',
meta: {title: '个人中心'}
}
]
}
]
export default new Router({
// mode: 'history',
base: '/',
scrollBehavior: () => ({y: 0}),
routes: constantRouterMap
})
可以看到,最终指向的views/login这个组件
4.login.vue
至此,我们看到了迷宫的入口!