当前位置:   article > 正文

vue3--element-plus-抽屉文件上传和富文本编辑器

vue3--element-plus-抽屉文件上传和富文本编辑器

一、封装组件

article/components/ArticleEdit.vue

  1. <script setup>
  2. import { ref } from 'vue'
  3. const visibleDrawer = ref(false)
  4. const open = (row) => {
  5. visibleDrawer.value = true
  6. console.log(row)
  7. }
  8. defineExpose({
  9. open
  10. })
  11. </script>
  12. <template>
  13. <!-- 抽屉 -->
  14. <el-drawer v-model="visibleDrawer" title="大标题" direction="rtl" size="50%">
  15. <span>Hi there!</span>
  16. </el-drawer>
  17. </template>

二、完善结构

1.准备数据

  1. const formModel = ref({
  2. title: '',
  3. cate_id: '',
  4. cover_img: '',
  5. content: '',
  6. state: ''
  7. })
  8. const open = async (row) => {
  9. visibleDrawer.value = true
  10. if (row.id) {
  11. console.log('编辑回显')
  12. } else {
  13. console.log('添加功能')
  14. }
  15. }

2.准备 form 表单结构

  1. import ChannelSelect from './ChannelSelect.vue'
  2. <template>
  3. <el-drawer
  4. v-model="visibleDrawer"
  5. :title="formModel.id ? '编辑文章' : '添加文章'"
  6. direction="rtl"
  7. size="50%"
  8. >
  9. <!-- 发表文章表单 -->
  10. <el-form :model="formModel" ref="formRef" label-width="100px">
  11. <el-form-item label="文章标题" prop="title">
  12. <el-input v-model="formModel.title" placeholder="请输入标题"></el-input>
  13. </el-form-item>
  14. <el-form-item label="文章分类" prop="cate_id">
  15. <channel-select
  16. v-model="formModel.cate_id"
  17. width="100%"
  18. ></channel-select>
  19. </el-form-item>
  20. <el-form-item label="文章封面" prop="cover_img"> 文件上传 </el-form-item>
  21. <el-form-item label="文章内容" prop="content">
  22. <div class="editor">富文本编辑器</div>
  23. </el-form-item>
  24. <el-form-item>
  25. <el-button type="primary">发布</el-button>
  26. <el-button type="info">草稿</el-button>
  27. </el-form-item>
  28. </el-form>
  29. </el-drawer>
  30. </template>

3.一打开默认重置添加的 form 表单数据

  1. const defaultForm = {
  2. title: '',
  3. cate_id: '',
  4. cover_img: '',
  5. content: '',
  6. state: ''
  7. }
  8. const formModel = ref({ ...defaultForm })
  9. const open = async (row) => {
  10. visibleDrawer.value = true
  11. if (row.id) {
  12. console.log('编辑回显')
  13. } else {
  14. console.log('添加功能')
  15. formModel.value = { ...defaultForm }
  16. }
  17. }

4.扩展 下拉菜单 width props

  1. defineProps({
  2. modelValue: {
  3. type: [Number, String]
  4. },
  5. width: {
  6. type: String
  7. }
  8. })
  9. <el-select
  10. ...
  11. :style="{ width }"
  12. >

三、上传文件模块

Element Plus 是一个基于 Vue 3 的组件库,它提供了一个 <ElUpload> 组件用于文件上传。以下是如何使用 Element Plus 的 <ElUpload> 组件进行文件上传的基本步骤:

  1. 引入 <ElUpload> 组件:在你的 Vue 组件中,你需要先引入 Element Plus 的 <ElUpload> 组件。

     

    import { Upload } from 'element-plus';

  2. 在模板中添加 <ElUpload>:在你的 Vue 组件的模板中,添加 <el-upload> 标签。

     

    <template> <el-upload :on-success="handleSuccess" :on-error="handleError" :before-upload="beforeUpload" action="http://your-upload-api.com" ref="upload" list-type="text"> <el-button slot="trigger" type="primary">选取文件</el-button> <el-button style="margin-left: 10px;" type="success" @click="submitUpload">上传到服务器</el-button> <div slot="tip" class="el-upload__tip">只能上传 jpg/png 文件,且不超过 500kb</div> </el-upload> </template>

  3. 定义方法:在你的 Vue 组件的 <script> 部分,定义处理上传成功、上传失败和文件上传前的钩子函数。

     

    export default { methods: { handleSuccess(response, file, fileList) { // 上传成功的处理逻辑 console.log('上传成功'); }, handleError(error, file, fileList) { // 上传失败的处理逻辑 console.error('上传失败'); }, beforeUpload(file) { // 上传文件之前的处理逻辑 const isLt2M = file.size / 1024 / 1024 < 2; if (!isLt2M) { this.$message.error('上传文件大小不能超过 2MB!'); } return isLt2M; }, submitUpload() { this.$refs.upload.submit(); // 触发表单提交,从而触发上传 }, }, }

  4. 配置属性

    • action:上传文件的服务器地址。
    • :on-success 和 :on-error:分别是上传成功和失败时的回调函数。
    • before-upload:文件上传前的钩子函数,可以在这里进行文件类型、大小等的校验。
    • ref="upload":为 <ElUpload> 组件设置一个引用,方便在方法中调用。
    • list-type="text":定义上传列表的展示方式,这里使用 text 类型,不展示文件列表。
  5. 触发上传:通常,上传动作是由用户点击按钮触发的,如上面的 submitUpload 方法所示。

  6. 样式和插槽:Element Plus 的 <ElUpload> 组件支持自定义触发按钮和上传列表,通过插槽(slot="trigger"slot="tip")可以灵活地插入自定义的 HTML。

  7. 注意事项:确保服务器端的上传接口可以处理上传的文件,并且遵守相应的安全规范。

1.关闭自动上传,准备结构

  1. import { Plus } from '@element-plus/icons-vue'
  2. <el-upload
  3. class="avatar-uploader"
  4. :auto-upload="false"
  5. :show-file-list="false"
  6. :on-change="onUploadFile"
  7. >
  8. <img v-if="imgUrl" :src="imgUrl" class="avatar" />
  9. <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  10. </el-upload>

2.准备数据 和 选择图片的处理逻辑

  1. const imgUrl = ref('')
  2. const onUploadFile = (uploadFile) => {
  3. imgUrl.value = URL.createObjectURL(uploadFile.raw)
  4. formModel.value.cover_img = uploadFile.raw
  5. }

3.样式美化

  1. .avatar-uploader {
  2. :deep() {
  3. .avatar {
  4. width: 178px;
  5. height: 178px;
  6. display: block;
  7. }
  8. .el-upload {
  9. border: 1px dashed var(--el-border-color);
  10. border-radius: 6px;
  11. cursor: pointer;
  12. position: relative;
  13. overflow: hidden;
  14. transition: var(--el-transition-duration-fast);
  15. }
  16. .el-upload:hover {
  17. border-color: var(--el-color-primary);
  18. }
  19. .el-icon.avatar-uploader-icon {
  20. font-size: 28px;
  21. color: #8c939d;
  22. width: 178px;
  23. height: 178px;
  24. text-align: center;
  25. }
  26. }
  27. }

element-plus下Update

四、富文本编辑器 [ vue-quill ]

、地址、https://vueup.github.io/vue-quill/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/540756
推荐阅读
相关标签
  

闽ICP备14008679号