一、首先須要按照Vue router支持css
npm install vue-router
而後須要在項目中引入:
import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter)
二、定義router的js文件vue
import Vue from 'vue' import Router from 'vue-router' import User from '../pages/user' import Home from '../pages/public/home' import Profile from '../pages/user/profile' import Form from '../pages/form' import Detail from '../pages/form/form' import File from '../pages/form/file' import Files from '../pages/file' Vue.use(Router) export default new Router({ routes: [ { path: '/', component:Home, children:[ { path: '/user', component:Profile}, { path: '/profile', component: User}, { path: '/form', component: Form}, { path: '/detail', component: Detail}, { path: '/profiles', component: Files}, { path: '/file', component: File} ] },
{ path: '/login', component:Login},
{ path: '/404', component:Error}
]
})
三、在main.js中引入routervue-router
import router from './router' new Vue({ router, render: h => h(App), }).$mount('#app')
四、入口頁面定義router-viewnpm
<div id="app"> <router-view></router-view> </div>
五、在path指向爲「/」的頁面中,定義頁面的佈局,例如:上(頭部)-中(左道航-右內容)-下(底部)。element-ui
<HeaderSection></HeaderSection> <div> <NavList class="nav"></NavList> <router-view class="router"></router-view> </div> <FooterSection></FooterSection>
六、左側導航,用elementUI實現,有一個NavMenu導航菜單,作導航功能。bash
在這裏提一下引入elementUI:markdown
(1)安裝app
npm i element-ui -S編輯器
(2)使用ide
在main.js中加入下面的代碼:
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
導航欄的代碼以下:
<el-menu class="sidebar-el-menu" :default-active="onRoutes" :collapse="collapse" background-color="#324157" text-color="#bfcbd9" active-text-color="#20a0ff" unique-opened router> <template v-for="item in items"> <template v-if="item.subs"> <el-submenu :index="item.index" :key="item.index"> <template slot="title"> <i :class="item.icon"></i><span slot="title">{{ item.title }}</span> </template> <template v-for="subItem in item.subs"> <el-submenu v-if="subItem.subs" :index="subItem.index" :key="subItem.index"> <template slot="title">{{ subItem.title }}</template> <el-menu-item v-for="(threeItem,i) in subItem.subs" :key="i" :index="threeItem.index"> {{ threeItem.title }} </el-menu-item> </el-submenu> <el-menu-item v-else :index="subItem.index" :key="subItem.index"> {{ subItem.title }} </el-menu-item> </template> </el-submenu> </template> <template v-else> <el-menu-item :index="item.index" :key="item.index"> <i :class="item.icon"></i><span slot="title">{{ item.title }}</span> </el-menu-item> </template> </template>
</el-menu>
定義左側導航的顯示和圖標等內容,index爲惟一標識,打開的是path路徑,對應router中的path,就能夠打開寫好的相應的頁面。
items: [ { icon: 'el-icon-share', index: 'user', title: '系統首頁' }, { icon: 'el-icon-time', index: 'profile', title: '基礎表格' }, { icon: 'el-icon-bell', index: '3', title: '表單相關', subs: [ { index: 'form', title: '基本表單' }, { index: '3-2', title: '三級菜單', subs: [ { index: 'detail', title: '富文本編輯器' }, { index: 'file', title: 'markdown編輯器' }, ] }, { index: 'profiles', title: '文件上傳' } ] }, ]
七、若是涉及到登陸頁面和不須要路由的頁面等,就須要在router的js文件中定義和「/」平級的其餘path的頁面,再判斷進入頁面是路由頁面仍是登陸等頁面。