欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue 嵌套路由使用總結(jié)(推薦)

 更新時(shí)間:2020年01月13日 14:10:58   作者:授客  
這篇文章主要介紹了Vue 嵌套路由使用總結(jié),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

開發(fā)環(huán)境

Win 10

node-v10.15.3-x64.msi

下載地址:

https://nodejs.org/en/

需求場景

如下圖,我們希望點(diǎn)擊導(dǎo)航欄不同菜單時(shí),導(dǎo)航欄下方加載不同的組件,進(jìn)而展示不同的頁面內(nèi)容 

解決方案

使用動(dòng)態(tài)路由

新建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">導(dǎo)航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; /*去掉前面的點(diǎn)*/
}
.nav ul li a {
 width: 100px;
 display: block; /*設(shè)置為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");
  }
 }

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

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

例中,觸發(fā)clickNav1()調(diào)用時(shí),提供的路徑字符串為“nav1”,為相對(duì)路徑,父級(jí)路由路徑為/index,所以點(diǎn)擊后跳轉(zhuǎn)的url路徑為/index/nav1。

例中,觸發(fā)clickHome()調(diào)用時(shí),提供的路徑字符串為“/index/home”,為絕對(duì)路徑,所以點(diǎn)擊后跳轉(zhuǎn)的url路徑為/index/home。

2、

<div class="content">
<router-view></router-view>
</div>

這里通過在父頁面,即index.vue組件中添加<router-view></router-view>實(shí)現(xiàn)動(dòng)態(tài)加載不同組件頁面。點(diǎn)擊導(dǎo)航菜單時(shí),會(huì)自動(dòng)加載對(duì)應(yīng)的組件,然后替換<router-view>元素為對(duì)應(yīng)的組件內(nèi)容。

參考鏈接:

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實(shí)現(xiàn)路由嵌套。個(gè)人理解,嵌套路由控制內(nèi)容子組件內(nèi)容的展示區(qū):實(shí)現(xiàn)父組件的內(nèi)容展示區(qū)保持不變,子組件內(nèi)容展示區(qū)動(dòng)態(tài)變化。

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

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

個(gè)人理解,執(zhí)行this.$router.push(path)后,程序自動(dòng)獲取需要跳轉(zhuǎn)的絕對(duì)url,暫且稱之為toPath,然后在routes中進(jìn)行匹配,如果匹配到則加載對(duì)應(yīng)的組件。

總結(jié)

通過router-view實(shí)現(xiàn)在當(dāng)前指定容器中動(dòng)態(tài)加載不同組件,展示不同頁面的大致實(shí)現(xiàn)思路:

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

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

以上所述是小編給大家介紹的Vue 嵌套路由使用總結(jié),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

最新評(píng)論