vue 在單頁(yè)面應(yīng)用里使用二級(jí)套嵌路由
在一個(gè)單頁(yè)面應(yīng)用里使用二級(jí)套嵌路由
目錄結(jié)構(gòu)如下:
其中main.js為全局配置文件,App.vue為項(xiàng)目入口。
main.js中路由配置如下
import Vue from ’vue’//引入vueimport App from ’./App’//引入主模板import Router from ’vue-router’// 引入router路由// 引入項(xiàng)目的模塊組件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’ },//默認(rèn)顯示home{ path: ’/home’, component: home,//路徑home的組件是home meta: { navShow: true}}, { path: ’/licai’, component: licai, meta: { navShow: true}}, { path: ’/wode’, component:wode, meta: { navShow: true}},{ path:’/home1/:num’, component:home1, meta: { navShow: false}},{ path:’/home2’, component:home2, meta: { navShow: false}, //這里定義了兩個(gè)子路由在home2模塊下 children:[ { path: ’/home2/home2_1’, component:home2_1}, { path: ’/home2/home2_2’, component:home2_2} ]}]// 實(shí)例化路由var vueRouter = new Router({ routes//此路由為上方定義})// 創(chuàng)建和掛載根實(shí)例new Vue({ el: ’#app’,//vue項(xiàng)目在哪個(gè)元素下 router: vueRouter,//使用路由 template: ’<App></App>’, components: { App }})
App.vue為主模板,也就是入口文件,其中定義的路由與一級(jí)路由無任何區(qū)別:
<template> <div id='app1'> <div v-show='$route.meta.navShow'> <!-- 引入公用的頭部 header組件 --> <v-header></v-header> </div> <div class='contianer'> <!-- 路由中的組件在這里被渲染,默認(rèn)被渲染的為home組件,已在路由配置中設(shè)置 --> <router-view></router-view> </div> </div></template>
home.vue,這里是首頁(yè),從這里可以進(jìn)入home2頁(yè)面:
<template> <div class='home box'> <h3>這里是home頁(yè)面</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='http://www.baoyu77737.com/static/img/png1.png'/></a>路由套嵌</header> <router-link to='/home2/home2_1'>子頁(yè)面1</router-link> <router-link to='/home2/home2_2'>子頁(yè)面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 在單頁(yè)面應(yīng)用里使用二級(jí)套嵌路由的詳細(xì)內(nèi)容,更多關(guān)于vue 使用二級(jí)嵌套路由的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 利用CSS3新特性創(chuàng)建透明邊框三角2. html清除浮動(dòng)的6種方法示例3. CSS代碼檢查工具stylelint的使用方法詳解4. Vue3使用JSX的方法實(shí)例(筆記自用)5. vue實(shí)現(xiàn)將自己網(wǎng)站(h5鏈接)分享到微信中形成小卡片的超詳細(xì)教程6. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)7. 詳解CSS偽元素的妙用單標(biāo)簽之美8. 使用css實(shí)現(xiàn)全兼容tooltip提示框9. JavaScript數(shù)據(jù)類型對(duì)函數(shù)式編程的影響示例解析10. 不要在HTML中濫用div
