感觉最不爽的地方,是vue-router的懒加载不够动态,需要事先把路径注册好。 也许vue-router的动态路由能实现的更好些,目前没有时间多做研究,先写个总结。
基本页面结构
Index页面做为主页面, 其它页面只是下载模板和相关的vue组件代码。 暂时加载组件使用了个全局函数,改成vue的方法也不难。
Index中引入jquery、vue、vue-router、element-ui相关的CSS和JS文件。
在Index中实现路由相关功能
const pages = {};
const routes = [
{ path: '/login', component: () => Promise.resolve(pages['login']) } ,
{ path: '/home', component: () => Promise.resolve(pages['home']) },
]
const router = new VueRouter({
routes: routes // (缩写) 相当于 routes: routes
})
const vueMain = new Vue({
el: "#app",
router:router
})
vueMain.$router.push('login');
function load(routePath) {
console.log(routePath)
if (typeof (pages[routePath]) != "un