1.我們繼續上一個案例 vue -- 路由精講制作導航 -- 從無到有 ,在 about文件夾下 創建一些文件夾。如下:
2. 編寫about.vue代碼。當我們點擊導航中 “關於我們” ,就會顯示該部分內容。代碼中寫了四個可供點擊后可以跳轉的模塊。和 <router-view></router-view> 表示你點擊了哪個組件,哪個組件就會渲染到這里來。
其中注意:css樣式,我們直接引入bootstrap中的導航的樣式,在標簽中直接添加class屬性的值就可以了。
about.vue代碼
1 <template> 2 <div> 3 <div class="row mb-5"> //row 代表行, mb-5 表示margin-bottom距離下面5 4 <!-- 導航 --> 5 <div class="col-4"> //四列 6 <div class="list-group mb-5"> 7 <router-link tag="li" class="nav-link" :to="{name:'historyLink'}"> 8 <a class="list-group-item list-group-item-action">歷史訂單</a> 9 </router-link> 10 <router-link tag="li" class="nav-link" :to="{name:'contactLink'}"> 11 <a class="list-group-item list-group-item-action">聯系我們</a> 12 </router-link> 13 <router-link tag="li" class="nav-link" :to="{name:'orderingGuideLink'}"> 14 <a class="list-group-item list-group-item-action">點餐文檔</a> 15 </router-link> 16 <router-link tag="li" class="nav-link" :to="{name:'deliveryLink'}"> 17 <a class="list-group-item list-group-item-action">快遞信息</a> 18 </router-link> 19 </div> 20 </div> 21 <!-- 導航所對應的內容 --> 22 <div class="col-8"> //8列 23 <router-view></router-view> 24 </div> 25 </div> 26 </div> 27 </template>
3.配置二級路由和三級路由
注意:我們要在about頁面下添加四個路由,用到 children 屬性, 而且還用到 redirect 屬性,默認跳轉到指定路由,具體操作看代碼和注釋。
main.js代碼
1 import Vue from 'vue' 2 import VueRouter from 'vue-router' 3 import App from './App.vue' 4 import Home from './components/Home.vue' 5 import Menu from './components/Menu.vue' 6 import Admin from './components/Admin.vue' 7 import About from './components/about/About.vue' 8 import Login from './components/Login.vue' 9 import Register from './components/Register.vue' 10 11 //二級路由 12 import Contact from './components/about/Contact.vue' 13 import Delivery from './components/about/Delivery.vue' 14 import History from './components/about/History.vue' 15 import OrderingGuide from './components/about/OrderingGuide.vue' 16 17 //三級路由 18 import Phone from './components/about/contact/Phone.vue' 19 import PersonName from './components/about/contact/PersonName.vue' 20 21 22 23 24 Vue.use(VueRouter) 25 核心代碼 二級路由和三級路由的跳轉 26 const routes = [ 27 {path:'/', name:'homeLink', component:Home}, 28 {path:'/menu', name:'menuLink', component:Menu}, 29 {path:'/admin', name:'adminLink', component:Admin}, 30 {path:'/about', name:'aboutLink', redirect:'/about/contact', component:About, children:[
//表示about頁面中默認跳轉到/about/contact 這個路由頁面下。 31 {path:'/about/contact', name:'contactLink', redirect:'/personName', component:Contact, children:[
//在/about/contact頁面中默認展現三級路由personName 的內容。 32 {path:'/phone', name:"phoneNumber", component:Phone}, 33 {path:'/personName', name:"personName", component:PersonName}, 34 ]}, 35 {path:'/history',name:'historyLink',component:History}, 36 {path:'/delivery',name:'deliveryLink',component:Delivery}, 37 {path:'/orderingGuide',name:'orderingGuideLink',component:OrderingGuide}, 38 ]}, 39 {path:'/login', name:'loginLink', component:Login}, 40 {path:'/register', name:'registerLink', component:Register}, 41 // {path:'*',redirect:'/'}, 42 ] 43 44 const router = new VueRouter({ 45 routes, 46 mode:'history' 47 }) 48 49 new Vue({ 50 el: '#app', 51 router, 52 render: h => h(App) 53 })
Contact.vue代碼
1 <template> 2 <div class="card text-dark bg-light mb-3"> 3 <div class="card-header">聯系我們</div> 4 <div class="card-body"> 5 <h4 class="card-title">聯系我們</h4> 6 <p class="card-text">1623487989@qq.com</p> 7 8 <router-link :to="{name:'phoneNumber'}">電話</router-link> 9 <router-link :to="{name:'personName'}">聯系人</router-link> 10 11 <router-view></router-view> 12 </div> 13 </div> 14 </template>
Delivery.vue代碼
1 <template> 2 <div class="card text-dark bg-light mb-3"> 3 <div class="card-header">快遞信息</div> 4 <div class="card-body"> 5 <h4 class="card-title">快遞信息</h4> 6 <p class="card-text">1623487989@qq.com</p> 7 </div> 8 </div> 9 </template>
History.vue代碼
1 <template> 2 <div class="card text-dark bg-light mb-3"> 3 <div class="card-header">歷史訂單</div> 4 <div class="card-body"> 5 <h4 class="card-title">歷史訂單</h4> 6 <p class="card-text">1623487989@qq.com</p> 7 </div> 8 </div> 9 </template>
OrderingGuide.vue代碼
1 <template> 2 <div class="card text-dark bg-light mb-3"> 3 <div class="card-header">點餐文檔</div> 4 <div class="card-body"> 5 <h4 class="card-title">點餐文檔</h4> 6 <p class="card-text">1623487989@qq.com</p> 7 </div> 8 </div> 9 </template>
Phone.vue代碼
<template> <h1>400040040404404</h1> </template>
PersonName.vue代碼
<template> <h1>小劭</h1> </template>