打开项目
引入
在Element官网复制粘贴到项目中
<template>
<div id="app">
<el-container style="height: 500px; border: 1px solid #eee">
<el-aside width="200px" style="background-color: rgb(238, 241, 246)">
<el-menu :default-openeds="['1', '3']">
<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>
<el-table :data="tableData">
<el-table-column prop="date" label="日期" width="140">
</el-table-column>
<el-table-column prop="name" label="姓名" width="120">
</el-table-column>
<el-table-column prop="address" label="地址">
</el-table-column>
</el-table>
</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 {
data() {
const item = {
date: '2016-05-02',
name: '王小虎',
address: '上海市普陀区金沙江路 1518 弄'
};
return {
tableData: Array(20).fill(item)
}
}
};
</script>
Vue 集成 Element UI
Element UI 后台管理系统主要的标签:
el-container:构建整个⻚⾯框架。
el-aside:构建左侧菜单。
el-menu:左侧菜单内容,常⽤属性:
1:default-openeds:默认展开的菜单,通过菜单的 index 值来关联。
<el-menu :default-openeds="['1', '3']">
2:default-active:默认选中的菜单,通过菜单的 index 值来关联。
<el-menu :default-openeds="['1', '3']" :default-active="'1-2'">
el-submenu:可展开的菜单,常⽤属性:
index:菜单的下标,⽂本类型,不能是数值类型。
template:对应 el-submenu 的菜单名。
i:设置菜单图标,通过 class 属性实则。
el-icon-messae
el-icon-menu
el-icon-setting
el-menu-item:菜单的⼦节点,不可再展开,常⽤属性:
index:菜单的下标,⽂本类型,不能是数值类型。
<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="1">
<template slot="title"><i class="el-icon-message"></i>导航一</template>
<el-menu-item index="1-1">选项1</el-menu-item>
<el-menu-item index="1-2">选项2</el-menu-item>
<el-menu-item index="1-3">选项3</el-menu-item>
<el-submenu index="1-4">
<template slot="title"><i class="el-icon-message"></i>选项4</template>
<el-menu-item index="1-4-1">选项4-1</el-menu-item>
<el-menu-item index="1-4-2">选项4-2</el-menu-item>
</el-submenu>
</el-submenu>
Vue router 来动态构建左侧菜单
导航1
1.⻚⾯1
2.⻚⾯2
导航2
1.⻚⾯3
2.⻚⾯4
在View首先创建页面
然后在router文件夹下的index.js配置路由
import Vue from 'vue'
import VueRouter from 'vue-router'
import PageOne from "../views/PageOne"
import PageTwo from "../views/PageTwo"
import PageThree from "../views/PageThree"
import PageFour from "../views/PageFour"
import App from "../App";
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: '导航1',
component: App,
},
{
path:'/pageOne',
name:'页面1',
component:PageOne
},
{
path:'/pageTwo',
name:'页面2',
component:PageTwo
},
{
path:'/pageThree',
name:'页面3',
component:PageThree
},
{
path:'/pageFour',
name:'页面4',
component:PageFour
}
]
测试http://localhost:8080/pageOne
不是想要的结果怎么办?
原因因为中间黄色部分的窗口没有开所以需要开窗口。
怎么开窗口?
在App.vue主页面中加<router-view></router-view>
代码如下
如何讲页面放到我们想要放的位置?
代码如下
菜单动态读取Router中的数据
router下的index.js
const routes = [
{
path: '/',
name: '导航1',
component: App,
child:[
{
path:'/pageOne',
name:'页面1',
component:PageOne
},
{
path:'/pageTwo',
name:'页面2',
component:PageTwo
}
]
},
{
path: '/navigation',
name: '导航2',
component: App,
child:[
{
path:'/pageThree',
name:'页面3',
component:PageThree
},
{
path:'/pageFour',
name:'页面4',
component:PageFour
}
]
}
app.vue怎么动态读取
获取index.js的routes
$router.options.routes
<div id="app">
<el-container style="height: 500px; border: 1px solid #eee">
<el-aside width="200px" style="background-color: rgb(238, 241, 246)">
<!-- <el-menu :default-openeds="['1', '3']" :default-active="'1-2'">-->
<!-- <el-submenu index="1">-->
<!-- <template slot="title"><i class="el-icon-message"></i>导航一</template>-->
<!-- <el-menu-item index="1-1">选项1</el-menu-item>-->
<!-- <el-menu-item index="1-2">选项2</el-menu-item>-->
<!-- <el-menu-item index="1-3">选项3</el-menu-item>-->
<!-- <el-submenu index="1-4">-->
<!-- <template slot="title"><i class="el-icon-message"></i>选项4</template>-->
<!-- <el-menu-item index="1-4-1">选项4-1</el-menu-item>-->
<!-- <el-menu-item index="1-4-2">选项4-2</el-menu-item>-->
<!-- </el-submenu>-->
<!-- </el-submenu>-->
<!-- </el-menu>-->
<el-menu>
<el-submenu v-for="(item,index) in $router.options.routes" :key="item.label" :index="index+''">
<template slot="title"><i class="el-icon-message"></i>{{item.name}}</template>
<el-menu-item v-for="item2 in item.children" :key="item2.label">{{item2.name}}</el-menu-item>
</el-submenu>
</el-menu>
</el-aside>
index代表下标 避免展开收回是冲突 index属性是字符串
如何把右侧页面只显示页面内容不嵌套导航
新建index.vue 将导航数据存放如下
index.vue
<template>
<el-container style="height: 500px; border: 1px solid #eee">
<el-aside width="200px" style="background-color: rgb(238, 241, 246)">
<!-- <el-menu :default-openeds="['1', '3']" :default-active="'1-2'">-->
<!-- <el-submenu index="1">-->
<!-- <template slot="title"><i class="el-icon-message"></i>导航一</template>-->
<!-- <el-menu-item index="1-1">选项1</el-menu-item>-->
<!-- <el-menu-item index="1-2">选项2</el-menu-item>-->
<!-- <el-menu-item index="1-3">选项3</el-menu-item>-->
<!-- <el-submenu index="1-4">-->
<!-- <template slot="title"><i class="el-icon-message"></i>选项4</template>-->
<!-- <el-menu-item index="1-4-1">选项4-1</el-menu-item>-->
<!-- <el-menu-item index="1-4-2">选项4-2</el-menu-item>-->
<!-- </el-submenu>-->
<!-- </el-submenu>-->
<!-- </el-menu>-->
<el-menu>
<el-submenu v-for="(item,index) in $router.options.routes" :key="item.label" :index="index+''">
<template slot="title"><i class="el-icon-message"></i>{{item.name}}</template>
<el-menu-item v-for="(item2,index2) in item.children" :key="item2.label" :index="index+'-'+index2">{{item2.name}}</el-menu-item>
</el-submenu>
</el-menu>
</el-aside>
<el-main>
<router-view></router-view>
</el-main>
</el-container>
</template>
<script>
export default {
name: "index"
}
</script>
<style scoped>
</style>
App.vue
<template>
<div id="app">
<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>
<!-- <router-view></router-view>-->
</div>
</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)
}
}
};
</script>
index.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import PageOne from "../views/PageOne"
import PageTwo from "../views/PageTwo"
import PageThree from "../views/PageThree"
import PageFour from "../views/PageFour"
// import App from "../App";
import index from "../views/index"
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: '导航1',
component: index,
children:[
{
path:'/pageOne',
name:'页面1',
component:PageOne
},
{
path:'/pageTwo',
name:'页面2',
component:PageTwo
}
]
},
{
path: '/navigation',
name: '导航2',
component: index,
children:[
{
path:'/pageThree',
name:'页面3',
component:PageThree
},
{
path:'/pageFour',
name:'页面4',
component:PageFour
}
]
}
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
export default router
menu 与 router 的绑定
1、 标签添加 router 属性。
2、在⻚⾯中添加 标签,它是⼀个容器,动态渲染你选择的 router。
3、 标签的 index 值就是要跳转的 router。
初始化初始页面
redirect:"/pageOne",
默认跳转
path: '/',
name: '导航1',
component: index,
redirect:"/pageOne",
children:[
{
path:'/pageOne',
name:'页面1',
component:PageOne
},
初始化没有被选中导航怎么办?
:class动态加样式
<el-menu-item v-for="(item2) in item.children" :key="item2.label" :index="item2.path"
:class="$route.path==item2.path?'is-active':''">{{item2.name}}</el-menu-item>