溫馨提示×

溫馨提示×

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

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

Vue實(shí)現(xiàn)路由嵌套的方法是什么

發(fā)布時間:2021-11-22 09:11:49 來源:億速云 閱讀:220 作者:柒染 欄目:開發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)Vue實(shí)現(xiàn)路由嵌套的方法是什么,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

1、嵌套路由又稱子路由,在實(shí)際應(yīng)用中,通常由多層嵌套的組件組合而成。(其實(shí)就是套娃操作罷了,跟后端的視圖跳轉(zhuǎn)路徑蠻像的):

Vue實(shí)現(xiàn)路由嵌套的方法是什么

2、 創(chuàng)建用戶信息組件,在 views/user 目錄下創(chuàng)建一個名為 Profile.vue 的視圖組件:

Profile.vue

<template>
  <h2>咸魚_翻身1</h2>
</template>
<script>
  export default {
    name: "UserList"
  }
</script>
<style scoped>
</style>

3、在用戶列表組件在 views/user 目錄下創(chuàng)建一個名為 List.vue 的視圖組件:

List.vue

<template>
  <h2>咸魚_翻身2</h2>
</template>
<script>
  export default {
    name: "UserList"
  }
</script>
<style scoped>
</style>

4、修改首頁視圖,我們修改 Main.vue 視圖組件,此處使用了 ElementUI 布局容器組件,代碼如下:

Main.vue

<template>
    <div>
      <el-container>
        <el-aside width="200px">
          <el-menu :default-openeds="['1']">
            <el-submenu index="1">
              <template slot="title"><i class="el-icon-caret-right"></i>用戶管理</template>
              <el-menu-item-group>
                <el-menu-item index="1-1">
                <!--插入的地方-->
                  <router-link to="/user/profile">個人信息</router-link>
                </el-menu-item>
                <el-menu-item index="1-2">
                <!--插入的地方-->
                  <router-link to="/user/list">用戶列表</router-link>
                </el-menu-item>
              </el-menu-item-group>
            </el-submenu>
            <el-submenu index="2">
              <template slot="title"><i class="el-icon-caret-right"></i>內(nèi)容管理</template>
              <el-menu-item-group>
                <el-menu-item index="2-1">分類管理</el-menu-item>
                <el-menu-item index="2-2">內(nèi)容列表</el-menu-item>
              </el-menu-item-group>
            </el-submenu>
          </el-menu>
        </el-aside>

        <el-container>
          <el-header >
            <el-dropdown>
              <i class="el-icon-setting" ></i>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>個人信息</el-dropdown-item>
                <el-dropdown-item>退出登錄</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-header>
          <el-main>
          <!--在這里展示視圖-->
            <router-view />
          </el-main>
        </el-container>
      </el-container>
    </div>
</template>
<script>
    export default {
        name: "Main"
    }
</script>
<style scoped lang="scss">
  .el-header {
    background-color: #B3C0D1;
    color: #333;
    line-height: 60px;
  }
  .el-aside {
    color: #333;
  }
</style>

5、配置嵌套路由修改 router 目錄下的 index.js 路由配置文件,使用children放入main中寫入子模塊,代碼如下:

index.js

//導(dǎo)入vue
import Vue from 'vue';
import VueRouter from 'vue-router';
//導(dǎo)入組件
import Main from "../views/Main";
import Login from "../views/Login";
//導(dǎo)入子模塊
import UserList from "../views/user/List";
import UserProfile from "../views/user/Profile";

//使用
Vue.use(VueRouter);
//導(dǎo)出
export default new VueRouter({
  routes: [
    {
      //登錄頁
      path: '/main',
      component: Main,
      //  寫入子模塊
      children: [
        {
          path: '/user/profile',
          component: UserProfile,
        }, {
          path: '/user/list',
          component: UserList,
        },
      ]
    },
    //首頁
    {
      path: '/login',
      component: Login

    },
  ]
})

6、運(yùn)行結(jié)果:

Vue實(shí)現(xiàn)路由嵌套的方法是什么

Vue實(shí)現(xiàn)路由嵌套的方法是什么

7、項(xiàng)目結(jié)構(gòu)為:

Vue實(shí)現(xiàn)路由嵌套的方法是什么

8、那么我們加一個功能呢:

Main.vue中加入這段代碼即可:

  <el-submenu index="3">
            <template slot="title"><i class="el-icon-caret-right"></i>咸魚_翻身管理</template>
            <el-menu-item-group>
              <el-menu-item index="3-1">咸魚_翻身4</el-menu-item>
              <el-menu-item index="3-2">咸魚_翻身5</el-menu-item>
            </el-menu-item-group>
          </el-submenu>

Vue實(shí)現(xiàn)路由嵌套的方法是什么

上述就是小編為大家分享的Vue實(shí)現(xiàn)路由嵌套的方法是什么了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(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)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI