vue-router中如何使用嵌套路由,針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

模板抽離
我們已經(jīng)學(xué)習(xí)過了Vue模板的另外定義形式,使用<template></template>。
<!-- 模板抽離出來 --> <template id="home"> <div>首頁</div> </template> <template id="news"> <div>新聞</div> </template>
然后js里定義路由組件的時候:
// 1. 定義(路由)組件。
const Home = { template: '#home' };
const News = { template: '#news' };路由嵌套
實際應(yīng)用界面,通常由多層嵌套的組件組合而成。
比如,我們 “首頁”組件中,還嵌套著 “登錄”和 “注冊”組件,那么URL對應(yīng)就是/home/login和/home/reg。
<template id="home"> <!-- 注意:組件只能有一個根元素,所以我們包裝到這個div中 --> <div> <h3>首頁</h3> <router-link to="/home/login">登錄</router-link> <router-link to="/home/reg">注冊</router-link> <!-- 路由匹配到的組件將渲染在這里 --> <router-view></router-view> </div> </template>
這是訪問/home后的模板,其中我們需要把/home/login和/home/reg渲染進來。
完成上面代碼后,HTML結(jié)構(gòu)如下圖:

登錄和注冊2個組件
<template id="login"> <div>登錄界面</div> </template> <template id="reg"> <div>注冊界面</div> </template>
//定義路由組件
const Login = { template: '#login' };
const Reg = { template: '#reg' };3.定義路由
// 2. 定義路由
const routes = [
{ path: '/', redirect: '/home' },
{
path: '/home',
component: Home,
children:[
{ path: '/home/login', component: Login},
{ path: '/home/reg', component: Reg}
]
},
{ path: '/news', component: News}
]注意我們在home路由配置了它的children。這就是嵌套路由。
4.案例全部代碼如下:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<script src="http://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
<div id="box">
<p>
<router-link to="/home">home</router-link>
<router-link to="/news">news</router-link>
</p>
<router-view></router-view>
</div>
<!-- 模板抽離出來 -->
<template id="home">
<!-- 注意:組件只能有一個根元素,所以我們包裝到這個div中 -->
<div>
<h3>首頁</h3>
<router-link to="/home/login">登錄</router-link>
<router-link to="/home/reg">注冊</router-link>
<!-- 路由匹配到的組件將渲染在這里 -->
<router-view></router-view>
</div>
</template>
<template id="news">
<div>新聞</div>
</template>
<template id="login">
<div>登錄界面</div>
</template>
<template id="reg">
<div>注冊界面</div>
</template>
<script type="text/javascript">
// 1. 定義(路由)組件。
const Home = { template: '#home' };
const News = { template: '#news' };
const Login = { template: '#login' };
const Reg = { template: '#reg' };
// 2. 定義路由
const routes = [
{ path: '/', redirect: '/home' },
{
path: '/home',
component: Home,
children:[
{ path: '/home/login', component: Login},
{ path: '/home/reg', component: Reg}
]
},
{ path: '/news', component: News}
]
// 3. 創(chuàng)建 router 實例,然后傳 `routes` 配置
const router = new VueRouter({
routes // (縮寫)相當(dāng)于 routes: routes
})
// 4. 創(chuàng)建和掛載根實例。
// 記得要通過 router 配置參數(shù)注入路由,
// 從而讓整個應(yīng)用都有路由功能
const app = new Vue({
router
}).$mount('#box')
// 現(xiàn)在,應(yīng)用已經(jīng)啟動了!
</script>
</body>
</html>
關(guān)于vue-router中如何使用嵌套路由問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道了解更多相關(guān)知識。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站chinadenli.net,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)站欄目:vue-router中如何使用嵌套路由-創(chuàng)新互聯(lián)
本文地址:http://chinadenli.net/article6/hjdig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、關(guān)鍵詞優(yōu)化、外貿(mào)建站、網(wǎng)站營銷、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容