vue 在單頁面應用裡使用二級套嵌路由
阿新 • • 發佈:2020-12-22
在一個單頁面應用裡使用二級套嵌路由
目錄結構如下:
其中main.js為全域性配置檔案,App.vue為專案入口。
main.js中路由配置如下
import Vue from 'vue'//引入vue import App from './App'//引入主模板 import Router from 'vue-router'// 引入router路由 // 引入專案的模組元件 import licai from './components/licai' import home from './components/home' import wode from './components/wode' import home1 from './components/home/home1' import home2 from './components/home/home2' import home2_1 from './components/home/home2_box/home2_1'//套嵌路由 import home2_2 from './components/home/home2_box/home2_2' Vue.use(Router)// 使用router // 定義路由 var routes = [ { path: '/',redirect: '/home' },//預設顯示home { path: '/home',component: home,//路徑home的元件是home meta: { navShow: true} },{ path: '/licai',component: licai,meta: { navShow: true} },{ path: '/wode',component:wode,{ path:'/home1/:num',component:home1,meta: { navShow: false} },{ path:'/home2',component:home2,meta: { navShow: false},//這裡定義了兩個子路由在home2模組下 children:[ { path: '/home2/home2_1',component:home2_1},{ path: '/home2/home2_2',component:home2_2} ] }] // 例項化路由 var vueRouter = new Router({ routes//此路由為上方定義 }) // 建立和掛載根例項 new Vue({ el: '#app',//vue專案在哪個元素下 router: vueRouter,//使用路由 template: '<App></App>',components: { App } })
App.vue為主模板,也就是入口檔案,其中定義的路由與一級路由無任何區別:
<template> <div id="app1"> <div class="nav-bottom" v-show="$route.meta.navShow"> <!-- 引入公用的頭部 header元件 --> <v-header></v-header> </div> <div class="contianer"> <!-- 路由中的元件在這裡被渲染,預設被渲染的為home元件,已在路由配置中設定 --> <router-view></router-view> </div> </div> </template>
home.vue,這裡是首頁,從這裡可以進入home2頁面:
<template> <div class="home box"> <h3>這裡是home頁面</h3> <router-link to="/home2">套嵌路由</router-link> </div> </template>
home2.vue,這裡可以展示套嵌路由了:
<template id="home2"> <div> <header class="home header"><a href="javascript:void(0);" rel="external nofollow" οnclick="javacript:window.history.go(-1)"><img src="../../../static/img/png1.png"/></a>路由套嵌</header> <router-link to="/home2/home2_1">子頁面1</router-link> <router-link to="/home2/home2_2">子頁面2</router-link> <!-- 路由匹配到的元件將渲染在這裡 --> <router-view></router-view> </div> </template> <style> .home.header{font-size:0.8rem;position:relative;} .home.header>a{display: block;height:0.8rem;width:0.4rem;margin-top:0.6rem;position:absolute;left:0.5rem;} .home.header>a>img{height:100%;width:100%;display:block;} </style>
效果:
以上就是vue 在單頁面應用裡使用二級套嵌路由的詳細內容,更多關於vue 使用二級巢狀路由的資料請關注我們其它相關文章!