vue+elemen ui实现左侧导航栏,右侧内容变化

一、首先编写导航栏组件

我的页面是大项目中的一部分,项目之前已经安装了路由等。

首先在component中添加guide.vue文件。
选择element的一个组件,示例代码如下

<template>
<el-container style="height: 500px; border: 1px solid #eee">
  <el-aside width="200px" style="background-color: rgb(238, 241, 246)">
    <el-menu :unique-opened="true" :default-active="$route.path"            @select="handleSelect">
      <el-submenu index="1">
        <template slot="title"><i class="el-icon-message"></i>导航一</template>
        <el-menu-item-group>
          <template slot="title">分组一</template>
          <el-menu-item index="1-1">选项1</el-menu-item>
          <el-menu-item index="1-2">选项2</el-menu-item>
        </el-menu-item-group>
        <el-menu-item-group title="分组2">
          <el-menu-item index="1-3">选项3</el-menu-item>
        </el-menu-item-group>
        <el-submenu index="1-4">
          <template slot="title">选项4</template>
          <el-menu-item index="1-4-1">选项4-1</el-menu-item>
        </el-submenu>
      </el-submenu>
      <el-submenu index="2">
        <template slot="title"><i class="el-icon-menu"></i>导航二</template>
        <el-menu-item-group>
          <template slot="title">分组一</template>
          <el-menu-item index="2-1">选项1</el-menu-item>
          <el-menu-item index="2-2">选项2</el-menu-item>
        </el-menu-item-group>
        <el-menu-item-group title="分组2">
          <el-menu-item index="2-3">选项3</el-menu-item>
        </el-menu-item-group>
        <el-submenu index="2-4">
          <template slot="title">选项4</template>
          <el-menu-item index="2-4-1">选项4-1</el-menu-item>
        </el-submenu>
      </el-submenu>
      <el-submenu index="3">
        <template slot="title"><i class="el-icon-setting"></i>导航三</template>
        <el-menu-item-group>
          <template slot="title">分组一</template>
          <el-menu-item index="3-1">选项1</el-menu-item>
          <el-menu-item index="3-2">选项2</el-menu-item>
        </el-menu-item-group>
        <el-menu-item-group title="分组2">
          <el-menu-item index="3-3">选项3</el-menu-item>
        </el-menu-item-group>
        <el-submenu index="3-4">
          <template slot="title">选项4</template>
          <el-menu-item index="3-4-1">选项4-1</el-menu-item>
        </el-submenu>
      </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 style="background-color: rgb(238, 241, 246)">           		<router-view></router-view>  
    </el-main>
    
   
  </el-container>
</el-container>

</template>
<style>
  .el-header {
    background-color: #B3C0D1;
    color: #333;
    line-height: 60px;
  }
  
  .el-aside {
    color: #333;
  }
</style>

<script>
  export default {
    data() {
      const item = {
        date: '2016-05-02',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      };
      return {
        tableData: Array(20).fill(item)
      }
    }
    methods:{      
    handleSelect(path){        
    this.$router.push(path)      
    },    
    }
  };
</script>

以上涉及到几个点
1. @select=“handleSelect”,handleSelect的默认参数是el-menu-item标签的index参数,如上面的1-1这些,真正项目中index可以命名为想要跳转页面的组件名,如page1
2. :default-active="$route.path" ,是使用户点击的那个页面展开
3. :unique-opened=“true”,始终只展开一个导航栏
4. 注意到上述代码中

 <el-main style="background-color: rgb(238, 241, 246)">               
 <router-view></router-view>  
 </el-main>

这是路由的出口,保证点击左侧的导航栏右侧显示相关内容。

二、右侧展示的页面示例

我们建立page1.vue,page2.vue(这里是示例,具体项目具体命名),假定把这两个组件也放在component文件夹下。

<template>    
<div class="top">        
<h3>我是page1,我要在右侧展示</h3>    
</div></template><script>  
export default {    
data() {      
return {}      
}    
}
</script>
<style>    
.top{        
text-align:center;    
}
</style>

同理写page2.vue

3.给他们添加路由

找到router目录下的index.js添加上述我们建立的三个页面的路由

import Guide from '@/components/Guide';
import page1 from '@/components/page1.vue';
import page2 from '@/components/page2';
Vue.use(Router);
export const basicRouters=[
{
path:'/Giude',
name:'Guide',
component:Giude,
children:[
{
path:'/page1',
name:'page1',
component:page1
},
{
path:'/page2',
name:'page2',
component:page2
}
]
}
]

可以看到,把想在右边显示的页面路由,放在了children中。这是使Guide成为这两个页面的父组件,这样就能是这两个页面显示左侧的导航栏。

发布了91 篇原创文章 · 获赞 28 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/gyx1549624673/article/details/101545183