最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue-router:嵌套路由的使用方法

 更新時(shí)間:2017年02月21日 16:28:54   作者:github_26672553  
本篇文章主要介紹了vue-router:嵌套路由的使用方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

模板抽離

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

  <!-- 模板抽離出來(lái) -->
  <template id="home">
    <div>首頁(yè)</div>
  </template>

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

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

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

路由嵌套

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

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

  <template id="home">
    <!-- 注意:組件只能有一個(gè)根元素,所以我們包裝到這個(gè)div中 -->
    <div>
      <h2>首頁(yè)</h2>
       <router-link to="/home/login">登錄</router-link>
      <router-link to="/home/reg">注冊(cè)</router-link>
      <!-- 路由匹配到的組件將渲染在這里 -->
      <router-view></router-view>
    </div>
  </template>

這是訪(fǎng)問(wèn)/home后的模板,其中我們需要把/home/login和/home/reg渲染進(jìn)來(lái)。

完成上面代碼后,HTML結(jié)構(gòu)如下圖:

這里寫(xiě)圖片描述

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

  <template id="login">
    <div>登錄界面</div>
  </template>
  <template id="reg">
    <div>注冊(cè)界面</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}
    ]

注意我們?cè)趆ome路由配置了它的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>

  <!-- 模板抽離出來(lái) -->
  <template id="home">
    <!-- 注意:組件只能有一個(gè)根元素,所以我們包裝到這個(gè)div中 -->
    <div>
      <h2>首頁(yè)</h2>
       <router-link to="/home/login">登錄</router-link>
      <router-link to="/home/reg">注冊(cè)</router-link>
      <!-- 路由匹配到的組件將渲染在這里 -->
      <router-view></router-view>
    </div>
  </template>

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

  <template id="login">
    <div>登錄界面</div>
  </template>
  <template id="reg">
    <div>注冊(cè)界面</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 // (縮寫(xiě))相當(dāng)于 routes: routes
    })


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

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

這里寫(xiě)圖片描述

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

北流市| 江陵县| 绍兴市| 屯昌县| 固始县| 伊宁县| 东辽县| 绩溪县| 西平县| 思茅市| 黔西| 武邑县| 瑞昌市| 崇左市| 天祝| 柳林县| 黄浦区| 启东市| 泰来县| 四子王旗| 福建省| 黄陵县| 通海县| 汶川县| 佛山市| 茌平县| 瑞安市| 泾阳县| 重庆市| 平原县| 易门县| 海兴县| 虎林市| 轮台县| 冕宁县| 永定县| 静海县| 雅江县| 衡阳市| 汾阳市| 抚顺市|