Vue 嵌套路由使用總結


Vue 嵌套路由使用總結

 

by:授客 QQ1033553122

 

開發環境

 

Win 10

 

node-v10.15.3-x64.msi

下載地址:

https://nodejs.org/en/

 

需求場景

如下圖,我們希望點擊導航欄不同菜單時,導航欄下方加載不同的組件,進而展示不同的頁面內容

 

 

 

 

 

解決方案

使用動態路由

新建home.vue作為子頁面組件

<template>

    <div>

        <h3>home Page</h3>

        <p>home page content</p>

    </div>

</template>

 

<script>

 

export default {

    name: "homePage",

};

</script>

 

<style scoped>

h3 {

    font-size: 30px;

}

</style>

 

新建nav1.vue作為子頁面組件

<template>

    <div>

        <h3>nav1 Page</h3>

        <p>nav1 page content</p>

    </div>

</template>

 

<script>

 

export default {

    name: "nav1Page",

 

};

</script>

 

<style scoped>

h3 {

    font-size: 30px;

}

</style>

 

新建index.vue作為父頁面

<template>

    <div class="container">

        <div class="nav">

            <ul>

                <li>

                    <a @click="clickHome">首頁</a>

                </li>

                <li>

                    <a @click="clickNav1">導航1</a>

                </li>

            </ul>

        </div>

        <div class="content">

            <router-view></router-view>

        </div>

    </div>

</template>

 

<script>

export default {

    methods: {

        clickHome() {

            this.$router.push("/index/home");

        },

        clickNav1() {

            this.$router.push("nav1");

        }

    }

};

</script>

 

<style>

.nav ul {

    width: 100%;

    height: 30px;

    margin: 5px;

    padding: 0;

}

 

.nav ul li {

    float: left; /*橫排顯示*/

    list-style-type: none; /*去掉前面的點*/

}

 

.nav ul li a {

    width: 100px;

    display: block; /*設置為block,width才起作用*/

    height: 28px;

    line-height: 28px;

    background: grey;

    color: #fff;

    margin: 0px 1px;

    font-size: 18px;

 

    text-align: center;

    text-decoration: none;

}

 

.nav ul li a:hover {

    width: 100px;

    height: 26px;

    line-height: 28px;

    border: 1px solid red;

    color: red;

    background: #fff;

}

 

.content {

    position: absolute;

    top: 40px;

    bottom: 0px;

    right: 10px;

    left: 15px;

    background: rgb(176, 207, 180)

}

</style>

 

說明:

1、

    methods: {

        clickHome() {

            this.$router.push("/index/home");

        },

        clickNav1() {

            this.$router.push("nav1");

        }

    }

 

點擊對應“首頁”菜單,“導航1”時分別觸發調用這里定義了兩個方法clickHome()和clickNav1(),兩個方法的實現都是調用this.$router.push(),航到不同的 UR(跳轉到不同的頁面)。另外,push這個方法會向 history 棧添加一個新的記錄,所以,當用戶點擊瀏覽器后退按鈕時,可以回到之前的頁面

 

需要注意的是,這里給push方法提供的代表路徑的字符串。如果該字符串不以“/”打頭,則表示相對路徑,相對於父級路由的path。如果該字符串以“/”打頭,則表示絕對路徑的,相對於根路徑“/”

 

例中,觸發clickNav1()調用時,提供的路徑字符串為“nav1”,為相對路徑,父級路由路徑為/index,所以點擊后跳轉的url路徑為/index/nav1。

例中,觸發clickHome()調用時,提供的路徑字符串為“/index/home”,為絕對路徑,所以點擊后跳轉的url路徑為/index/home。

 

2、

<div class="content">

<router-view></router-view>

</div>

 

這里通過在父頁面,即index.vue組件中添加<router-view></router-view>實現動態加載不同組件頁面。點擊導航菜單時,會自動加載對應的組件,然后替換<router-view>元素為對應的組件內容。

 

 

參考鏈接:

https://router.vuejs.org/zh/guide/essentials/navigation.html

https://router.vuejs.org/zh/guide/essentials/nested-routes.html

 

修改router/index.js

import Vue from "vue"

import Router from "vue-router"

 

 

import index from "@/views/index"

import home from "@/views/home"

import nav1 from "@/views/nav1"

 

Vue.use(Router)

 

export default new Router({

  mode: "history",

  routes: [

    {

      path: "/index",

      name: "index",

      component: index,

      children: [

        {

          path: "/index/home",

          name: "home",

          component: home

        },

        {

          path: "nav1",

          name: "nav1",

          component: nav1

        }

      ]

    }

  ]

})

 

 

說明:

1、vue路由通過children實現路由嵌套。個人理解,嵌套路由控制內容子組件內容的展示區:實現父組件的內容展示區保持不變,子組件內容展示區動態變化。

 

2、同this.$router.push(path),這里的path也分相對路徑(相對於父級路由的path路徑),和絕對路徑(相對於“/”)。如上,/index/home為絕對路徑,nav1為相對路徑(其絕對路徑為/index/nav1)。注意,這里path是否為絕對路徑,不影響是否嵌套路由,是否嵌套路由,是通過children決定的,只是影響路由匹配。如上,如果path: "nav1",寫成path: "/nav1",,那么執行this.$router.push("nav1")時,跳轉的url為/index/nav1,將找不到匹配的路由

 

3、this.$router.push(path) 和這里routes的關系:

個人理解,執行this.$router.push(path)后,程序自動獲取需要跳轉的絕對url,暫且稱之為toPath,然后在routes中進行匹配,如果匹配到則加載對應的組件。

總結

通過router-view實現在當前指定容器中動態加載不同組件,展示不同頁面的大致實現思路:

1、 在當前頁面(這里稱之為父頁面).vue文件template模板中的指定位置(“包含”子組件內容的容器),添加<router-view></router-view>元素

2、 router/index.js中給父頁面路徑所在的路由,添加子路由:子組件的加載url對應的路由


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM