Element-Plus的安装和使用
前言
这篇文章介绍了Element-Plus的安装和使用,Element-Plus的底层是基于vue的,所以需要搭配vue3来使用。
Element-Plus的安装
1.1使用包管理器下载
# 选择一个你喜欢的包管理器
# NPM
$ npm install element-plus --save
# Yarn
$ yarn add element-plus
# pnpm
$ pnpm install element-plus
1.2 直接通过浏览器的 HTML 标签导入 Element Plus
<head>
<!-- Import style -->
<link rel="stylesheet" href="//unpkg.com/element-plus/dist/index.css" />
<!-- Import Vue 3 -->
<script src="//unpkg.com/vue@3"></script>
<!-- Import component library -->
<script src="//unpkg.com/element-plus"></script>
</head>
2 vue项目引入Element-Plus
在vue项目中的main.js文件中引入
/* 引入Element-Plus插件 */
import ElementPlus from 'element-plus'
/* 引入Element-Plus样式 */
import 'element-plus/dist/index.css'
/* 使用Element-Plus插件 */
app.use(ElementPlus)
3、做完上述两个步骤后就可以使用Element-Plus了
(1)按钮的使用
A 基础按钮
<template>
<el-row class="mb-4">
<el-button type="primary">点击</el-button>
<el-button type="success">点击成功</el-button>
<el-button type="info">进入</el-button>
<el-button type="warning">等待</el-button>
<el-button type="danger">删除</el-button>
</el-row>
</template>
<script>
name:'App'
</script>
<style>
</style>
B 禁用按钮
<template>
<el-row class="mb-4">
<el-button disabled>Default</el-button>
<el-button type="primary" disabled>Primary</el-button>
<el-button type="success" disabled>Success</el-button>
<el-button type="info" disabled>Info</el-button>
<el-button type="warning" disabled>Warning</el-button>
<el-button type="danger" disabled>Danger</el-button>
</el-row>
</template>
<script>
name:'App'
</script>
<style>
</style>
C 链接按钮
<div class="flex justify-space-between mb-4 flex-wrap gap-4">
<el-button
v-for="button in buttons"
:key="button.text"
:type="button.type"
link
>{{ button.text }}</el-button
>
</div>
D 图标按钮
<template>
<div class="flex">
<el-button type="primary" :icon="Edit" />
<el-button type="primary" :icon="Share" />
<el-button type="primary" :icon="Delete" />
<el-button type="primary" :icon="Search">Search</el-button>
<el-button type="primary">
Upload<el-icon class="el-icon--right"><Upload /></el-icon>
</el-button>
</div>
</template>
<script setup >
import {
Delete,
Edit,
Search,
Share,
Upload } from '@element-plus/icons-vue'
</script>
<style>
</style>
(2)布局容器
使用规则
-
< el-container >:外层容器。 当子元素中包含 或 时,全部子元素会垂直上下排列, 否则会水平左右排列。
-
< el-header >:顶栏容器。
-
< el-aside >:侧边栏容器。
-
< el-main >:主要区域容器。
-
< el-footer >:底栏容器。
注:以上组件采用了flex
布局,使用前请确定目标浏览器是否兼容。 此外, < el-container >的直接子元素必须是后四个组件中的一个或多个。 后四个组件的亲元素必须是一个 < el-container >
常见布局
<template>
<div class="common-layout">
<el-container>
<el-header class="lay01">Header</el-header>
<el-main class="lay02">Main</el-main>
</el-container>
</div>
</template>
<script>
</script>
<style>
.lay01{
background: black;
color: white;
text-align: center;
}
.lay02{
background: skyblue;
text-align: center;
}
</style>
============================================================================
<template>
<div class="common-layout">
<el-container>
<el-header class="lay01">Header</el-header>
<el-main class="lay02">Main</el-main>
<el-footer class="lay03">Footer</el-footer>
</el-container>
</div>
</template>
<script>
</script>
<style>
.lay01{
background: black;
color: white;
text-align: center;
}
.lay02{
background: skyblue;
text-align: center;
}
.lay03{
background: blueviolet;
text-align: center;
}
</style>
============================================================================
<template>
<div class="common-layout">
<el-container>
<el-header class="lay01">Header</el-header>
<el-container>
<el-aside width="200px" class="lay02">Aside</el-aside>
<el-main class="lay03">Main</el-main>
</el-container>
</el-container>
</div>
</template>
<script>
</script>
<style>
.lay01{
background: black;
color: white;
text-align: center;
}
.lay02{
background: skyblue;
text-align: center;
}
.lay03{
background: blueviolet;
text-align: center;
}
</style>
============================================================================
<template>
<div class="common-layout">
<el-container>
<el-aside width="200px" class="lay01">Aside</el-aside>
<el-container>
<el-header class="lay02">Header</el-header>
<el-main class="lay03">Main</el-main>
<el-footer class="lay04">Footer</el-footer>
</el-container>
</el-container>
</div>
</template>
<script>
</script>
<style>
.lay01{
background: black;
color: white;
text-align: center;
}
.lay02{
background: skyblue;
text-align: center;
}
.lay03{
background: blueviolet;
text-align: center;
}
.lay04{
background: yellowgreen;
text-align: center;
}
</style>
(3)Layout 布局
通过基础的 24 分栏
,迅速简便地创建布局。
例如:
//span属性把左边分成了4等份,把中间分成了16等份,把右边分成了4等份
<template>
<el-row>
<el-col :span="4"><div class="grid-content ep-bg-purple" />
<div class="lay01"><p>左边</p></div>
</el-col>
<el-col :span="16"><div class="grid-content ep-bg-purple-light" />
<div class="lay02"><p>中间</p></div>
</el-col>
<el-col :span="4"><div class="grid-content ep-bg-purple" />
<div class="lay03"><p>右边</p></div>
</el-col>
</el-row>
</template>
<style >
.lay01{
background: yellow;
text-align: center;
}
.lay02{
background: greenyellow;
text-align: center;
}
.lay03{
background: black;
text-align: center;
color: white;
}
</style>
行提供 gutter 属性来指定列之间的间距,其默认值为0。
//设置列之间的间距为20
<el-row :gutter="20">
通过制定 col 组件的 offset 属性可以指定分栏偏移的栏数。
<el-col :span="6" :offset="6">
(4)响应式布局
-
xs (Extra Small)
- 非常小的屏幕:这通常适用于手机或非常小的设备,例如小型智能手机。在这个屏幕尺寸下,页面布局可能需要进行特殊的调整,以适应较小的显示空间。 -
sm (Small)
- 小型屏幕:这适用于较小的平板电脑和一些较旧的手机。在这个屏幕尺寸下,页面布局可能需要进行一些适配,以确保内容可以清晰地显示和使用。 -
md(Medium)
- 中等尺寸屏幕:这适用于标准大小的平板电脑和较大的移动设备。在这个屏幕尺寸下,页面布局通常较为标准化,不需要过多的调整。 -
lg(Large)
- 大型屏幕:这适用于较大的台式机显示器、笔记本电脑和某些大型平板电脑。在这个屏幕尺寸下,页面布局可以更加宽敞,展示更多的内容和功能。 -
xl (Extra Large)
- 非常大的屏幕:这适用于大型显示屏、高分辨率的电视和投影设备。在这个屏幕尺寸下,页面布局可以更宽阔,并利用更多的显示空间来提供更丰富的体验。
<template>
<el-row :gutter="20">
<el-col :xs="8" :sm="10" :md="8" :lg="6" :xl="1"><div class="grid-content ep-bg-purple" />
<div class="lay01">first</div>
</el-col>
<el-col :span="6" :offset="6" :xs="8" :sm="10" :md="8" :lg="3" :xl="1" >
<div class="grid-content ep-bg-purple"/>
<div class="lay02">second</div>
</el-col>
</el-row>
</template>
<style>
.el-row {
margin-bottom: 20px;
}
.el-row:last-child {
margin-bottom: 0;
}
.el-col {
border-radius: 4px;
}
.grid-content {
border-radius: 4px;
min-height: 36px;
}
.lay01{
background: yellow;
}
.lay02{
background: grey;
}
</style>
(5)基于断点的隐藏类
Element Plus 额外提供了一系列类名,用于在某些条件下隐藏元素。 这些类名可以添加在任何 DOM 元素或自定义组件上。 如果需要,请自行引入以下文件:
import 'element-plus/theme-chalk/display.css'
类名
hidden-xs-only
- 当视口在 xs 尺寸时隐藏
hidden-sm-only
- 当视口在 sm 尺寸时隐藏
hidden-sm-and-down
- 当视口在 sm 及以下尺寸时隐藏
hidden-sm-and-up
- 当视口在 sm 及以上尺寸时隐藏
hidden-md-only
- 当视口在 md 尺寸时隐藏
hidden-md-and-down
- 当视口在 md 及以下尺寸时隐藏
hidden-md-and-up
- 当视口在 md 及以上尺寸时隐藏
hidden-lg-only
- 当视口在 lg 尺寸时隐藏
hidden-lg-and-down
- 当视口在 lg 及以下尺寸时隐藏
hidden-lg-and-up
- 当视口在 lg 及以上尺寸时隐藏
hidden-xl-only
- 当视口在 xl 尺寸时隐藏
(6)表格和分页
设置layout,表示需要显示的内容,用逗号分隔,布局元素会依次显示。 prev表示上一页,next为下一页,pager表示页码列表,除此以外还提供了jumper和total,size和特殊的布局符号->,->后的元素会靠右显示,jumper表示跳页元素,total表示总条目数,size用于设置每页显示的页码数量。
<template>
<div class="example-pagination-block">
<div class="example-demonstration">When you have few pages</div>
<el-pagination layout="prev, pager, next" :total="50" />
</div>
<div class="example-pagination-block">
<div class="example-demonstration">When you have more than 7 pages</div>
<el-pagination layout="prev, pager, next" :total="1000" />
</div>
</template>
<style scoped>
.example-pagination-block + .example-pagination-block {
margin-top: 10px;
}
.example-pagination-block .example-demonstration {
margin-bottom: 16px;
}
</style>
更多请访问:Element-Plus
总结
Element-Plus给我们提供了一些强大的组件,让我们项目开发更快速,更便捷,学会使用Element-Plus将帮助我们更好的就业。