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

Vue2之嵌套路由詳解

 更新時間:2025年08月26日 09:44:13   作者:xrkhy  
本文介紹Vue嵌套路由的實現(xiàn)方式,通過動態(tài)URL路徑嵌套組件展示,涉及修改路由配置及創(chuàng)建子組件Son1/Son2,結(jié)合ElementUI菜單組件應(yīng)用

數(shù)據(jù)準備

  • src/router/index.js
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  routes: [
    // 配置重定向
    { path: '/', redirect: '/find' },
    { path: '/find', component: ()=> import('@/views/Find.vue')},
    { path: '/my', component: () => import('@/views/My.vue')},
    { path: '/friend', component:  () => import('@/views/Friend.vue')}
  ]
})

export default router
  • src/app.vue
<template>
  <div>
    <div class="footer_wrap">
      <router-link to="/find?id=1">發(fā)現(xiàn)音樂</router-link>
      <router-link to="/my">我的音樂</router-link>
      <router-link to="/friend">朋友</router-link>
    </div>
    <div class="top">
      <!-- 路由出口 → 匹配的組件所展示的位置 -->
      <router-view></router-view>
    </div>

  </div>
</template>

<script>
export default {

}
</script>

<style lang="scss">
body {
  margin: 0;
  padding: 0;
}
.footer_wrap {
  position: relative;
  left: 0;
  top: 0;
  display: flex;
  width: 100%;
  text-align: center;
  background-color: #333;
  color: #ccc;
}
.footer_wrap a {
  flex: 1;
  text-decoration: none;
  padding: 20px 0;
  line-height: 20px;
  background-color: #333;
  color: #ccc;
  border: 1px solid black;
}

//  路由激活樣式
.footer_wrap .router-link-active{
  background-color: purple;
}
</style>
  • src/views/Find.vue
<template>
  <div>
    <p>發(fā)現(xiàn)音樂</p>
    <p>發(fā)現(xiàn)音樂</p>
    <p>發(fā)現(xiàn)音樂</p>
    <p>發(fā)現(xiàn)音樂</p>
  </div>
</template>

<script>
export default {
  name: 'FindMusic'
}
</script>

<style>

</style>
  • src/views/Friend.vue
<template>
  <div>
    <p>我的朋友</p>
    <p>我的朋友</p>
    <p>我的朋友</p>
    <p>我的朋友</p>
  </div>
</template>

<script>
export default {
  name: 'MyFriend'
}
</script>

<style>

</style>
  • src/views/my.vue
<template>
  <div>
    <p>我的音樂</p>
    <p>我的音樂</p>
    <p>我的音樂</p>
    <p>我的音樂</p>
  </div>
</template>

<script>
export default {
  name: 'MyMusic'
}
</script>

<style>

</style>

項目目錄

運行結(jié)果

什么是嵌套路由

通過路由實現(xiàn)組件的嵌套展示,叫做嵌套路由。

實際生活中的應(yīng)用界面,通常由多層嵌套的組件組合而成。同樣地,URL 中各段動態(tài)路徑也按某種結(jié)構(gòu)對應(yīng)嵌套的各層組件

語法

語法1(推薦)

// ...
const router = new VueRouter({
  // ...
  routes: [
    // 配置重定向
    { path: '/', redirect: '/find' },
    { path: '/find', component: ()=> import('@/views/Find.vue'),
      // 配置子路由的重定向
      redirect: '/find/son1',
      // 配置子路由,子路由前面沒有/。這種寫法沒有冗余
      children:[
        { path: 'son1', component: () => import('@/views/find/Son1.vue')},
        { path: 'son2', component: () => import('@/views/find/Son2.vue')},
      ]
    },
    { path: '/my', component: () => import('@/views/My.vue')},
    { path: '/friend', component:  () => import('@/views/Friend.vue')}
  ]
})
// ...

語法2

// ...
const router = new VueRouter({
  // ...
  routes: [
    // 配置重定向
    { path: '/', redirect: '/find' },
    { path: '/find', component: ()=> import('@/views/Find.vue'),
      // 配置子路由
      children:[
        // 配置子路由的重定向  這種寫法前面有冗余
        { path: '/find',redirect: '/find/son1'},
        { path: '/find/son1', component: () => import('@/views/find/Son1.vue')},
        { path: '/find/son2', component: () => import('@/views/find/Son2.vue')},
      ]
    },
    { path: '/my', component: () => import('@/views/My.vue')},
    { path: '/friend', component:  () => import('@/views/Friend.vue')}
  ]
})
// ...

代碼演示

  • 新建src/views/find/Son1.vue
<template>
  <div style="border: 1px solid red; padding: 20px">
    <h3>我是find的子路由son1</h3>
  </div>
  
</template>

<script>
export default {
}
</script>

<style>
</style>
  • 新建src/views/find/Son2.vue
<template>
  <div  style="border: 1px solid red; padding: 20px">
    <h3>我是find的子路由son2</h3>
  </div>
  
</template>

<script>
export default {

}
</script>

<style>
</style>
  • 修改src/router/index.js(第一種語法)
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  routes: [
    // 配置重定向
    { path: '/', redirect: '/find' },
    { path: '/find', component: ()=> import('@/views/Find.vue'),
      // 配置子路由的重定向
      redirect: '/find/son1',
      // 配置子路由
      children:[
        { path: 'son1', component: () => import('@/views/find/Son1.vue')},
        { path: 'son2', component: () => import('@/views/find/Son2.vue')},
      ]
    },
    { path: '/my', component: () => import('@/views/My.vue')},
    { path: '/friend', component:  () => import('@/views/Friend.vue')}
  ]
})

export default router

  • 修改Find.vue
<template>
  <div>
  	<!-- 添加二級路由 -->
    <div class="childernNav">
      <router-link to="/find/son1">子組件1</router-link>
      <router-link to="/find/son2">子組件2</router-link>
    </div>

	<!-- 添加二級路由的視圖 -->
    <router-view></router-view>
    <p>發(fā)現(xiàn)音樂</p>
    <p>發(fā)現(xiàn)音樂</p>
    <p>發(fā)現(xiàn)音樂</p>
    <p>發(fā)現(xiàn)音樂</p>
  </div>
</template>

<script>
export default {
  name: "FindMusic",
};
</script>

<style lang="scss">
.childernNav {
  display: flex;
  margin: 20px 0 ;
  a {
    // 清除a標簽的默認樣式
    text-decoration: none;
    color: #000;
    display: block;
    width: 100px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    background-color: #ccc;
  }
  .router-link-active {
    background-color: green;
  }
}
</style>

運行流程

運行結(jié)果

結(jié)合ElementUI實戰(zhàn)

請參考我之前的博客ElementUI之菜單(Menu)使用

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解VUE Element-UI多級菜單動態(tài)渲染的組件

    詳解VUE Element-UI多級菜單動態(tài)渲染的組件

    這篇文章主要介紹了VUE Element-UI多級菜單動態(tài)渲染的組件,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Vue數(shù)據(jù)與事件綁定以及Class和Style的綁定詳細講解

    Vue數(shù)據(jù)與事件綁定以及Class和Style的綁定詳細講解

    這篇文章主要介紹了Vue數(shù)據(jù)與事件綁定以及Class和Style的綁定,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-01-01
  • vue實現(xiàn)多個el-form表單提交統(tǒng)一校驗的2個方法

    vue實現(xiàn)多個el-form表單提交統(tǒng)一校驗的2個方法

    這篇文章主要給大家介紹了關(guān)于vue實現(xiàn)多個el-form表單提交統(tǒng)一校驗的2個方法,文中通過代碼示例介紹的非常詳細,對大家學(xué)習(xí)或使用vue具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07
  • Vue?3?進階用法之異步組件

    Vue?3?進階用法之異步組件

    為了解決加載組件中出現(xiàn)的報錯、超時、狀態(tài)展示等問題,可以使用?Vue?3?提供的異步組件(Async?Components),它對于加載過程做了更細致的控制,這篇文章主要介紹了Vue?3?進階用法之異步組件,需要的朋友可以參考下
    2024-04-04
  • vue自定義開關(guān)組件使用詳解

    vue自定義開關(guān)組件使用詳解

    這篇文章主要為大家詳細介紹了vue自定義開關(guān)組件的使用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 解決Vue的文本編輯器 vue-quill-editor 小圖標樣式排布錯亂問題

    解決Vue的文本編輯器 vue-quill-editor 小圖標樣式排布錯亂問題

    這篇文章主要介紹了解決Vue的文本編輯器 vue-quill-editor 小圖標樣式排布錯亂問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Vue之請求如何傳遞參數(shù)

    Vue之請求如何傳遞參數(shù)

    這篇文章主要介紹了Vue之請求如何傳遞參數(shù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Vue3+TypeScript項目架構(gòu)實踐指南

    Vue3+TypeScript項目架構(gòu)實踐指南

    在現(xiàn)代前端開發(fā)中,Vue3和Vite的組合已經(jīng)成為許多開發(fā)者的首選,這篇文章主要介紹了Vue3+TypeScript項目架構(gòu)實踐的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2026-02-02
  • 在Vue3中實現(xiàn)懶加載功能的代碼示例

    在Vue3中實現(xiàn)懶加載功能的代碼示例

    在現(xiàn)代前端開發(fā)中,懶加載是一種提高應(yīng)用性能和用戶體驗的重要技術(shù),尤其是在處理較大圖片或長列表數(shù)據(jù)時,本文將使用 Vue 3 和其新推出的 setup 語法糖來實現(xiàn)懶加載功能,并提供具體的示例代碼,需要的朋友可以參考下
    2024-09-09
  • Vue一個案例引發(fā)的遞歸組件的使用詳解

    Vue一個案例引發(fā)的遞歸組件的使用詳解

    這篇文章主要介紹了Vue一個案例引發(fā)的遞歸組件的使用,本文主要給大家展示如何在項目中使用遞歸組件,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-11-11

最新評論

沂源县| 赤峰市| 曲靖市| 衡东县| 古蔺县| 普洱| 无棣县| 青田县| 开阳县| 望江县| 黄石市| 靖西县| 双鸭山市| 南平市| 婺源县| 铁岭县| 成武县| 尉氏县| 济南市| 类乌齐县| 青冈县| 洪泽县| 上饶市| 托克托县| 屏东县| 台湾省| 上饶县| 阿拉善右旗| 长沙市| 彭泽县| 巩留县| 肃宁县| 冷水江市| 嘉善县| 那坡县| 托克托县| 延寿县| 凤凰县| 邻水| 宁陵县| 花垣县|