文章目录
1. 配置使用 Element
- 安装 Element:在项目路径下终端键入以下命令:
npm i element-ui -S
- (全局引入)引入并使用 Element:在
index.js
中通过 import 导入,通过 Vue.use(xxx) 全局使用。
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI)
- (按需引入)引入并使用 Element:在
index.js
中通过 import 导入指定组件,通过 Vue.use(xxx) 使用该组件。
import {
Button} from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(Button)
并在
babel.config.js
中写入以下代码
"plugins": [
[
"component",
{
"libraryName": "element-ui",
"styleLibraryName": "theme-chalk"
}
]
]
- 通过终端键入
npm run build
,可以发现全局引入和按需引入,生成的打包文件 dist 的大小差距很大。按需引入,会减少大概四分之一
的体积。
2. 路由的使用
- 安装路由,终端键入:
npm i vue-router@3
- 创建 router 文件,创建 index.js 进行路由相关配置
import Vue from "vue";
import VueRouter from "vue-router";
import Home from '../views/Home.vue'
import User from '../views/User.vue'
Vue.use(VueRouter);
const routes = [
{
path: "/",
name: "Home",
component: Home,
},
{
path: '/user',
name: 'User',
component: User
}
];
const router = new VueRouter({
mode: "history",
routes,
});
export default router;
- 在 main.js 导入并使用路由
import router from '../router'
new Vue({
render: h => h(App),
router
}).$mount('#app')
- 在 HelloWorld 组件配置 router-link(组件支持用户在具有路由功能的应用中点击导航)
<router-link to="/">
<el-button>按钮</el-button>
</router-link>
<router-link to="/user">
<el-button type="primary">主要按钮</el-button>
</router-link>
- 在 App 组件相应位置显示内容
<router-view to="/"></router-view>
3. 首页的搭建
- 安装 less 插件:
npm i less
- 安装 插件:
npm i [email protected]
- 对 Home.vue 写入布局
<el-container style="hight: 100%">
<el-aside width="auto">Aside</el-aside>
<el-container>
<el-header>Header</el-header>
<el-main>Main</el-main>
</el-container>
</el-container>
- 对 Home.vue 设置样式
<style lang="less" scoped>
.el-header{
background-color: #333;
}
.el-main{
padding: 0;
}
</style>
- 记得在 main.js 中按需引入
import {
Button, Radio, Container, Main, Header, Aside} from 'element-ui';
Vue.use(Button)
Vue.use(Radio)
Vue.use(Container)
Vue.use(Main)
Vue.use(Header)
Vue.use(Aside)
4. 左侧菜单栏的引入
- component 文件夹下创建 CommonAside.vue,写入布局及样式
<template>
<el-menu
default-active="1-4-1"
class="el-menu-vertical-demo"
@open="handleOpen"
@close="handleClose"
:collapse="isCollapse"
>
<el-submenu index="1">
<template slot="title">
<i class="el-icon-location"></i>
<span slot="title">导航一</span>
</template>
<el-menu-item-group>
<span slot="title">分组一</span>
<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">
<span slot="title">选项4</span>
<el-menu-item index="1-4-1">选项1</el-menu-item>
</el-submenu>
</el-submenu>
<el-menu-item index="2">
<i class="el-icon-menu"></i>
<span slot="title">导航二</span>
</el-menu-item>
<el-menu-item index="3" disabled>
<i class="el-icon-document"></i>
<span slot="title">导航三</span>
</el-menu-item>
<el-menu-item index="4">
<i class="el-icon-setting"></i>
<span slot="title">导航四</span>
</el-menu-item>
</el-menu>
</template>
<script>
export default {
data() {
return {
isCollapse: true
};
},
methods: {
handleOpen(key, keyPath) {
console.log(key, keyPath);
},
handleClose(key, keyPath) {
console.log(key, keyPath);
}
}
}
</script>
<style lang="less" scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
width: 200px;
min-height: 400px;
}
</style>
- 记得在 main.js 中按需引入
5. 一级菜单实现
- 在 CommonAside.vue 的 data 中添加数据
menu: [
{
path: "/",
name: "home",
label: "首页",
icon: "s-home",
url: "Home/Home",
},
{
path: "/mall",
name: "mall",
label: "商品管理",
icon: "video-play",
url: "MallManage/MallManage",
},
{
path: "/user",
name: "user",
label: "用户管理",
icon: "user",
url: "UserManage/UserManage",
},
{
label: "其他",
icon: "location",
children: [
{
path: "/page1",
name: "page1",
label: "页面1",
icon: "setting",
url: "Other/PageOne",
},
{
path: "/page2",
name: "page2",
label: "页面2",
icon: "setting",
url: "Other/PageTwo",
},
],
},
]
- 在计算属性 computed 中定义函数,过滤出没有子级的一级菜单
noChildren() {
return this.menu.filter((item) => !item.children);
}
- v-for 遍历过滤出来的一级菜单,并在相应位置做出呈现
<el-menu-item v-for="item in noChildren" :index="item.path" :key="item.path">
<i :class="'el-icon-' + item.icon"></i>
<span slot="title">{
{
item.label}}</span>
</el-menu-item>
6. 二级菜单实现
- 在计算属性 computed 中定义函数,过滤出有子级的菜单
hasChildren() {
return this.menu.filter((item) => item.children);
}
- v-for 遍历过滤出来的二级菜单,并在相应位置做出呈现
<el-submenu v-for="item in hasChildren" :index="item.path" :key="item.path">
<template slot="title">
<i :class="'el-icon-' + item.icon"></i>
<span slot="title">{
{
item.label}}</span>
</template>
<!-- 二级菜单 -->
<el-menu-item-group v-for="(subItem, subIndex) in item.children" :index="subIndex" :key="subItem.path">
<el-menu-item >{
{
subItem.label}}</el-menu-item>
</el-menu-item-group>
</el-submenu>
7. menu 样式和路由跳转
- CommonAside.vue 中设置样式
background-color="#545c64"
text-color="#fff"
active-text-color="#ffd04b"
......
.el-menu {
height: 100vh;
border: #fff;
h3 {
color: white;
text-align: center;
line-height: 48px;
}
}
- 路由配置
<el-menu-item @click='clickMenu(item)'></el-menu-item>
.......
clickMenu(item) {
this.$router.push({
name: item.name
})
}
8. header 组件搭建
- 引入菜单按钮,大写设置为 mini
- 引入下拉栏,trigger 设置点击时下拉
<div class="l-content">
<el-button plain icon="el-icon-menu" size="mini"></el-button>
<h3 style="color: #fff">首页</h3>
</div>
<div class="r-content">
<el-dropdown trigger="click" size="mini">
<span>
<img class="user" :src="userImg" />
</span>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item>个人中心</el-dropdown-item>
<el-dropdown-item>退出</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</div>
- 通过 flex 布局书写样式,调整好布局
header {
display: flex;
height: 100%;
justify-content: space-between;
align-items: center;
}
.l-content{
display: flex;
align-items: center;
.el-button {
margin-right: 20px;
}
}
.r-content {
.user{
width: 40px;
height: 40px;
border-radius: 50%;
}
}
不积跬步无以至千里 不积小流无以成江海
点个关注不迷路(持续更新中…)