为什么通过路由显示的子组件显示的位置不对?

依依雨柔
  • 233

index.vue:

<template>
   <div>
    <el-row class="container">
      <el-col :span="24" class="topbar-wrap">
        <div class="topbar-logo topbar-btn">
          <a href="/"><img src="../assets/logo.png"></a>
        </div>
        <div class="topbar-logos">
            <a href="/"><img src="../assets/logotxt.png"></a>
        </div>
        <div class="topbar-title">
            <span>后台管理系统</span>
        </div>
        <el-dropdown class="dropdown">
          <span class="el-dropdown-link">
            管理员<i class="iconfont">&#xe649;</i>
          </span>
          <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>
      </el-col>
      <el-col :span="24" class="main">
         <el-menu
         router
      :default-active="this.$router.path"
      class="el-menu-vertical-demo"
      >
      <el-menu-item index="/">
        <i class="iconfont">&#xe681;</i>
        <span slot="title">首页</span>
      </el-menu-item>
      <el-menu-item index="/userlist">
          <i class="iconfont">&#xe60a;</i>
          <span slot="title">用户列表</span>
      </el-menu-item>
      <el-submenu index="/book">
        <template slot="title">
          <i class="iconfont white">&#xe62a;</i>
          <span slot="title" class="white">图书管理</span>
        </template>
        <el-menu-item-group>
          <el-menu-item index="/book/list">图书列表</el-menu-item>
          <el-menu-item index="/book/category">图书分类</el-menu-item>
        </el-menu-item-group>
      </el-submenu>
      <el-submenu index="/set">
        <template slot="title">
          <i class="iconfont">&#xe62b;</i>
          <span slot="title" class="white">设置</span>
        </template>
        <el-menu-item-group>
          <el-menu-item index="/set/personinfo">个人信息</el-menu-item>
          <el-menu-item index="/set/changepwd">修改密码</el-menu-item>
        </el-menu-item-group>
      </el-submenu>
    </el-menu>
        <el-col :span="24" class="content-wrapper">
          <transition name="fade" mode="out-in">
            <router-view></router-view> 
          </transition>
        </el-col>
      </el-col>
    </el-row>
   </div>
</template>
<script>
export default {
  data () {
    return {
      name: 'Home'
    }
  }
}
</script>
<style lang="scss" scoped>
</style>

userList.vue:


 <template>
    <el-row class="wrap">
        <el-col :span="24" class="warp-breadcrum">
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>用户列表</el-breadcrumb-item>
           </el-breadcrumb>
        </el-col>

        <el-col :span="24" class="warp-min" v-loading="loading"  element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
        >
            <el-col :span="24" class="toolbar">
                <el-form :inline="true" :model="formInline" class="demo-form-inline" align="left">
                   <el-input v-model="formInline.user" placeholder="用户名/姓名/昵称" style="min-width: 240px;"></el-input>
                    <el-button type="primary" @click="onSubmit">查询</el-button>
                </el-form>
            </el-col>

             <el-table
                :data="users"
                highlight-current-row
                v-loading="loading"
                align="center">
                    <el-table-column    
                        prop="name"
                        label="姓名"
                        width="120"
                        sortable>
                    </el-table-column>
                    <el-table-column
                        prop="nickname"
                        label="昵称"
                        width="120"
                        sortable>
                    </el-table-column>
                    <el-table-column
                        prop="sex"
                        label="性别"
                        width="100"
                        sortable>
                    </el-table-column>
                    <el-table-column
                        prop="email"
                        label="邮箱"
                        width="160"
                        sortable>
                    </el-table-column>
                    <el-table-column
                        prop="address"
                        label="地址"
                        sortable>
                    </el-table-column>
                </el-table>
        </el-col>

    </el-row>
</template>
<script>
export default {
  name: 'UserList',
  data() {
      return {
        loading: false,
        formInline: {
          user: ''
        },
        users: [{
            nickname: '小虎',
            name: '王小虎',
            sex: '男',
            email: '[email protected]',
            address: '上海市普陀区金沙江路 1518 弄'
          }, {
            nickname: '小虎',
            name: '王小虎',
            sex: '男',
            email: '[email protected]',
            address: '上海市普陀区金沙江路 1518 弄'
          }, {
           nickname: '小虎',
            name: '王小虎',
            sex: '男',
            email: '[email protected]',
            address: '上海市普陀区金沙江路 1518 弄'
          }, {
           nickname: '小虎',
            name: '王小虎',
            sex: '男',
            email: '[email protected]',
            address: '上海市普陀区金沙江路 1518 弄'
          }]
      }
    },
    methods: {
      onSubmit() {
        console.log('submit!');
      }
    }
}
</script>
<style lang="scss" scoped>
    .demo-form-inline{
        margin-top: 20px;
    }
    .el-input{
        width: 10%
    }
</style>

clipboard.png点击“用户列表”结果如下:

clipboard.png
为什么只显示了子组件,index.vue中的导航栏等怎么不显示?想要的效果如下:

clipboard.png

怎么解决

回复
阅读 1.9k
2 个回答
✓ 已被采纳

你这个是路由配置的问题,如果想实现外层结构的话,需要嵌套路由。手机回答见谅

{

  path: '/index',
  component: Layout,
  children: [
    {
      path: 'user,
      component: User
    }
  ]
}

写个公用的home组件, 引入 侧边栏、头部、主要内容区、底部 组件,
路由写到 内容区 试试

 <v-head></v-head>
        <v-sidebar></v-sidebar>
        <div class="content-box" :class="{'content-collapse':collapse}">
            <v-tags></v-tags>
            <div class="content">
                <transition name="move" mode="out-in">
                    <router-view></router-view>
                </transition>
            </div>
            <v-footer></v-footer>
        </div>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏