欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue router-view的嵌套显示实现

程序员文章站 2022-10-20 21:57:14
目录一、路由配置const routes = [ { path: '/', name: '导航1', component: home, children:[ {...

一、路由配置

const routes = [
  {
    path: '/',
    name: '导航1',
    component: home,
    children:[
      {
        path: '/customer',
        name: 'customer',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackchunkname: "about" */ '../views/customer.vue')
      },
      {
        path: '/pageone',
        name: '页面1',
        component: pageone,

      },
      {
        path: '/pagetwo',
        name: '页面2',
        component: pagetwo,
    },
    ]
  },
  {
    path: '/navigation',
    name: '导航2',
    component: home,
    children:[
      {
        path: '/pagethree',
        name: '页面3',
        component: pagethree,

      },
      {
        path: '/pagefour',
        name: '页面4',
        component: pagefour
      },
    ]
  },

二、vue页面嵌套

app.vue先配置第一个router-view

// an highlighted block
 <router-view></router-view>

home.vue配置第二个router-view

// an highlighted block
<template>
  <div>
  <el-container style="height: 500px; border: 1px solid #eee">
    <el-aside width="200px" style="background-color: rgb(238, 241, 246)">

      <el-menu>
        <el-submenu v-for="(item,index) in $router.options.routes" :index="index+''">
        <template slot="title"><i class="el-icon-sell"></i>{{item.name}}</template>
          <el-menu-item v-for="(item2,index2) in item.children" :index="index+'-'+index2">{{item2.name}}</el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>

    <el-container>
      <el-header style="text-align: right; font-size: 12px">
        <el-dropdown>
          <i class="el-icon-setting" style="margin-right: 15px"></i>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>查看</el-dropdown-item>
            <el-dropdown-item>新增</el-dropdown-item>
            <el-dropdown-item>删除</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <span>王小虎</span>
      </el-header>

      <el-main>
        <router-view></router-view>

      </el-main>
    </el-container>

  </el-container>

</div>
</template>

<style>
.el-header {
  background-color: #b3c0d1;
  color: #333;
  line-height: 60px;
}

.el-aside {
  color: #333;
}
</style>

<script>
export default {

};
</script>

三、嵌套联系

首先,在访问http://localhost:8181/时会进入第一层嵌套,此时进入第一个router-view:home.vue。然后当访问pageone时,会连带home.vue继续访问。

因为router-view的嵌套显示和路由路基的嵌套有关,可以看到,在路由里面,导航一的路径底下分别是页面一以及页面二的路由路径。所以当访问页面一pageone时,会先访问上级路径home.vue页面。加入home.vue页面没有放置router-view,那么下级页面将无法显示

到此这篇关于vue router-view的嵌套显示实现的文章就介绍到这了,更多相关vue router-view嵌套显示内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!