在使用 Element Plus 之前,需要先创建一个 Vue 工程,具体可以参考上一篇文章。
创建好后就可以按照官方文档中的指示,在工程目录下安装 Element 依赖:
npm install element-plus --save
注意,Element 和 Element Plus 是两个不同的项目,前者适用于 Vue 2.X,后者适用于 Vue 3.X。这里使用的是后者。
按照官方文档的指引,修改main.js
的内容:
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
const app = createApp(App)
app.use(ElementPlus)
app.mount('#app')
这里的import ElementPlus from 'element-plus'
导入了 Element Plus 模块,import 'element-plus/dist/index.css'
导入了相关的样式。
完整引入后就可以在 vue 文件中使用 ElementPlus 的组件了,比如修改App.vue
:
<template>
<el-row class="mb-4">
<el-button>Default</el-button>
<el-button type="primary">Primary</el-button>
<el-button type="success">Success</el-button>
<el-button type="info">Info</el-button>
<el-button type="warning">Warning</el-button>
<el-button type="danger">Danger</el-button>
</el-row>
</template>
<script setup>
</script>
<template>
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="title" label="文章标题" />
<el-table-column prop="category" label="分类" />
<el-table-column prop="time" label="发表时间" />
<el-table-column prop="state" label="状态" />
<el-table-column label="操作" width="180">
<el-row>
<el-button type="primary" :icon="Edit" circle />
<el-button type="danger" :icon="Delete" circle />
</el-row>
</el-table-column>
</el-table>
</template>
<script lang="ts" setup>
import {
Delete,
Edit,
} from '@element-plus/icons-vue'
const tableData = [
{
title: '标题1',
category: '时事',
time: '2023-01-01',
state: '已发布',
},
{
title: '标题1',
category: '时事',
time: '2023-01-01',
state: '已发布',
},
{
title: '标题1',
category: '时事',
time: '2023-01-01',
state: '已发布',
},
]
</script>
<template>
<div class="demo-pagination-block">
<el-pagination
class="el-p"
v-model:current-page="currentPage4"
v-model:page-size="pageSize4"
:page-sizes="[5, 10, 15, 20]"
:small="small"
:disabled="disabled"
:background="background"
layout="jumper, total, sizes, prev, pager, next"
:total="total"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const currentPage4 = ref(1)
const pageSize4 = ref(5)
const small = ref(false)
const background = ref(false)
const disabled = ref(false)
const total = ref(100)
const handleSizeChange = (val: number) => {
console.log(`${val} items per page`)
}
const handleCurrentChange = (val: number) => {
console.log(`current page: ${val}`)
}
</script>
<style scoped>
.el-p{
margin-top: 20px;
display: flex;
justify-content: flex-end;
}
</style>
关于分页条的详细说明可以观看这个视频。
<template>
<el-form :inline="true" :model="formInline" class="demo-form-inline">
<el-form-item label="文章分类">
<el-select v-model="formInline.category" placeholder="请选择" clearable>
<el-option label="时事" value="时事" />
<el-option label="小说" value="小说" />
</el-select>
</el-form-item>
<el-form-item label="发布状态">
<el-select v-model="formInline.state" placeholder="请选择" clearable>
<el-option label="已发布" value="已发布" />
<el-option label="草稿" value="草稿" />
</el-select>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="onSubmit">搜索</el-button>
</el-form-item>
<el-form-item>
<el-button type="default" @click="reset">重置</el-button>
</el-form-item>
</el-form>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
const formInline = reactive({
category: '',
state: ''
})
const onSubmit = () => {
console.log('submit!')
}
const reset = () => {
}
</script>
关于表单的详细说明可以观看这个视频。
<template>
<el-card class="box-card">
<template #header>
<div class="card-header">
<span>文章管理</span>
<el-button type="primary">发布文章</el-button>
</div>
</template>
<div>
<!-- 卡片内容 -->
</div>
</template>
<style scoped>
.card-header{
display: flex;
justify-content: space-between;
}
</style>
关于卡片的详细说明可以观看这个视频。
谢谢阅读,本文的完整示例代码见这里。