Vue3 + Element Plus 常用代码片段
566 字
3 分钟
Vue3 + Element Plus 常用代码片段
一、el-table 添加序号
在 el-table 中添加从 1 开始的序号列,支持分页连续编号。
template:
<el-table-column label="序号" align="center" type="index" :index="indexMethod"/>script:
indexMethod(index) { let pageNum = this.queryParams.pageNum - 1; if ((pageNum !== -1 && pageNum !== 0)) { return (index + 1) + (pageNum * this.queryParams.pageSize); } else { return (index + 1) }},二、el-upload 上传图片
使用 el-upload 组件实现图片上传、格式校验与回显。
template:
<el-form-item label="现场照片" prop="urls"> <el-upload v-model:file-list="imgFileList1" :action="action" :headers="imgToken" :on-success="handleSuccess1" list-type="picture-card" :before-upload="beforeAvatarUpload" :on-preview="handlePictureCardPreview" :on-remove="handleRemove1" > <el-icon> <Plus /> </el-icon> </el-upload></el-form-item>data:
imgFileList1: [],imgFileListForm: [],piclist1: [],imgToken: { 'Blade-Auth': 'Bearer ' + JSON.parse(localStorage.getItem('saber-token')).content},limitImg: 10,action: `/api/blade-resource/oss/endpoint/put-file`,disabled: false,dialogImageUrl: '',dialogVisible: falsemethods:
beforeAvatarUpload(file) { const imgType = file.type === 'image/jpeg' || file.type === 'image/png'; const imgSize = file.size / 1024 / 1024 < 5; if (!imgType) { this.$utils.toast('图片只能是 JPG/PNG 格式!', 'error'); } if (!imgSize) { this.$utils.toast('图片大小不能超过 5MB!', 'error'); } return imgType && imgSize;},
handlePictureCardPreview(uploadFile) { this.dialogImageUrl = uploadFile.url; this.dialogVisible = true;},
handleRemove1(res) { let index = this.imgFileListForm.findIndex(e => e === res.url); this.imgFileListForm.splice(index, 1);},
handleSuccess1(res) { if (res.code === 200) { this.imgFileListForm.push(res.data.link); }},图片回显:
if (this.form.images) { this.form.images.forEach(value => { this.imgFileList1.push({ url: value }); });}三、动态添加输入框并校验
利用 Vue3 响应式能力,在 el-table 中动态添加输入框并实现实时校验。
<template> <div> <el-form :model="info" ref="forms"> <el-table ref="tableRef" :data="info.data" border> <el-table-column align="center" property="name" label="*姓名"> <template #default="row"> <el-form-item :prop="'data.' + row.$index + '.name'" :rules="formRules.name"> <el-input placeholder="请输入姓名" v-model="info.data[row.$index].name" /> </el-form-item> </template> </el-table-column> <el-table-column align="center" property="role" label="角色"> <template #default="row"> <el-form-item :prop="'data.' + row.$index + '.role'" :rules="formRules.role"> <el-input placeholder="请输入角色" v-model="info.data[row.$index].role" /> </el-form-item> </template> </el-table-column> </el-table> </el-form> <el-button type="primary" @click="submitForm()">Submit</el-button> </div></template>
<script setup lang="ts">import { ref, reactive } from 'vue'import type { FormInstance } from 'element-plus'
let info: any = reactive({ data: [ { id: 0, name: '', role: '' }, { id: 1, name: '', role: '' } ]})
const formRules = reactive({ name: [{ required: true, message: '请输入姓名', trigger: 'change' }], role: [{ required: true, message: '请输入角色', trigger: 'change' }]})
const forms = ref<FormInstance>()const submitForm = async () => { if (!forms) return return await forms.value?.validate((valid: any) => { if (valid) { console.log('submit!') } else { console.log('error submit!') return false } })}</script>四、flv.js 播放 FLV 视频流
在 Vue3 中使用 flv.js 播放 HTTP-FLV 在线视频流。
npm install vue flv.js --save<template> <div> <video ref="videoPlayer" controls width="640" height="360"></video> </div></template>
<script>import flvjs from 'flv.js';
export default { data() { return { flvPlayer: null, videoSource: 'http://example.com/live/stream.flv' }; }, mounted() { this.initFlvPlayer(); }, beforeDestroy() { this.destroyFlvPlayer(); }, methods: { initFlvPlayer() { if (flvjs.isSupported()) { const videoPlayer = this.$refs.videoPlayer; const flvPlayer = flvjs.createPlayer({ type: 'flv', url: this.videoSource }); flvPlayer.attachMediaElement(videoPlayer); flvPlayer.load(); flvPlayer.play(); this.flvPlayer = flvPlayer; } else { console.error('FLV.js is not supported in this browser.'); } }, destroyFlvPlayer() { if (this.flvPlayer) { this.flvPlayer.destroy(); } } }};</script>支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!
Vue3 + Element Plus 常用代码片段
https://blog.olinl.com/posts/vue3-snippets/ 相关文章 智能推荐
1
Nginx 使用 sub_filter 注入自定义 HTML 标签
服务与应用运维 通过 Nginx 的 ngx_http_sub_module 模块,在反向代理响应中注入自定义 JS、CSS 或 HTML 标签,适用于无法修改源码的第三方页面定制场景。
2
为了护住我那几块硬盘:我的 UPS 监控“三部曲
HomeLab 私有云 从 PVE 备注里的简陋脚本,到独立的 Node.js Web 监控页,记录一个 HomeLab 玩家的 UPS 监控进阶之路。
3
CI/CD 手册:Jenkins 自动化发布全流程
服务与应用运维 详细记录如何通过 Jenkins 搭建自动化流水线,实现 Node.js 前端项目与 Spring Boot 后端项目的打包、上传及一键部署。
4
《地球 Online》:这破游戏我是一天也待不下去了
碎影与杂谈 这是一篇关于《地球 Online》删档内测版的深度锐评。画质拉满但平衡性极差,随机性太强且无法存档。策划出来挨打!欢迎踊跃发言!
5
一个字一个字敲出来的文章,为什么没人看?——写给“自闭”中的自己
碎影与杂谈 记录一次深夜的创作者“自闭”:当你掏心掏肺写出的硬核干货石沉大海,该如何面对这种让人窒息的失落感?
随机文章 随机推荐