溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

vue-router里怎么使用嵌套路由

發(fā)布時(shí)間:2022-11-01 10:02:58 來源:億速云 閱讀:132 作者:iii 欄目:開發(fā)技術(shù)

今天小編給大家分享一下vue-router里怎么使用嵌套路由的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

模板抽離

我們已經(jīng)學(xué)習(xí)過了Vue模板的另外定義形式,使用<template></template>。

  <!-- 模板抽離出來 -->
  <template id="home">
    <div>首頁</div>
  </template>

  <template id="news">
    <div>新聞</div>
  </template>

然后js里定義路由組件的時(shí)候:

// 1. 定義(路由)組件。
    const Home = { template: '#home' };
    const News = { template: '#news' };

路由嵌套

實(shí)際應(yīng)用界面,通常由多層嵌套的組件組合而成。

比如,我們 “首頁”組件中,還嵌套著 “登錄”和 “注冊”組件,那么URL對應(yīng)就是/home/login和/home/reg。

  <template id="home">
    <!-- 注意:組件只能有一個(gè)根元素,所以我們包裝到這個(gè)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渲染進(jìn)來。

登錄和注冊2個(gè)組件

  <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">
    <!-- 注意:組件只能有一個(gè)根元素,所以我們包裝到這個(gè)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 實(shí)例,然后傳 `routes` 配置
    const router = new VueRouter({
      routes // (縮寫)相當(dāng)于 routes: routes
    })


    // 4. 創(chuàng)建和掛載根實(shí)例。
    // 記得要通過 router 配置參數(shù)注入路由,
    // 從而讓整個(gè)應(yīng)用都有路由功能
    const app = new Vue({
     router
    }).$mount('#box')

    // 現(xiàn)在,應(yīng)用已經(jīng)啟動(dòng)了!
  </script>
</body>
</html>

Vue的優(yōu)點(diǎn)

Vue具體輕量級(jí)框架、簡單易學(xué)、雙向數(shù)據(jù)綁定、組件化、數(shù)據(jù)和結(jié)構(gòu)的分離、虛擬DOM、運(yùn)行速度快等優(yōu)勢,Vue中頁面使用的是局部刷新,不用每次跳轉(zhuǎn)頁面都要請求所有數(shù)據(jù)和dom,可以大大提升訪問速度和用戶體驗(yàn)。

以上就是“vue-router里怎么使用嵌套路由”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI