vue3上传图片(组件上传)

vue相较于js更加便利,尤其是其提供的组件;

今天分享一个vue3的图片上传组件

import { ElMessageBox } from "element-plus";

先引入组件;先看html部分

<el-upload
            list-type="picture-card"
            :action="'https://*******'"//接口
            :on-change="handleChange"
            :before-remove="beforeRemove"
            :on-preview="handlePictureCardPreview"
            :file-list="fileList.front_file"
            multiple
            limit="1"//限制上传数量
            name="img"//参数
          >
<el-icon class="avatar-uploader-icon">
              <Plus />
            </el-icon>
          </el-upload>

接下来是js部分

// 上传图片
const fileList = ref([]);
const handleChange = file => {
  if (file.status == "success") {
    fileList.value = [];
    fileList.value.push(file.response);
    console.log(fileList);
    console.log(fileList.value[0].front_file);
  }
};
// 删除
const beforeRemove = () => {
  const result = new Promise((resolve, reject) => {
    ElMessageBox.confirm("此操作将删除该图片, 是否继续?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    })
      .then(() => {
        resolve();
        upload_btn.value = false;
      })
      .catch(() => {
        reject(false);
      });
  });
  return result;
};

看看效果

 

因为我只需要上传一张所以加了一个限制,上传成功之后隐藏上传按钮

在html中放入

:class="{ hide_box: upload_btn }"
            :on-success="handleSuccess"

 在js中声明

const upload_btn = ref(false);

最后在css中放入


.hide_box /deep/ .el-upload--picture-card {
  display: none;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TinyMCE Vue 是一个将 TinyMCE(一个流行的富文本编辑器)集成到 Vue.js 应用中的组件。要在 Vue3 中使用 TinyMCE 配合上传图片,首先你需要安装相关的依赖和配置TinyMCE。以下是一个简单的示例: 1. 安装依赖: ```bash npm install tinymce @tinymce/tinymce-vue vue-file-upload --save ``` 2. 在 main.js 或者 vue.config.js 中引入并配置Vue文件上传插件(这里使用vue-file-upload为例): ```javascript import VueFileUpload from 'vue-file-upload'; import '@tinymce/tinymce-vue/dist/tinymce.vue'; Vue.use(VueFileUpload); ``` 3. 在组件中引入TinyMCE,并配置图片上传功能: ```html <template> <div> <tinymce-vue :config="tinymceConfig" v-model="editorContent" @file-added="handleImageUpload" ></tinymce-vue> </div> </template> <script> export default { data() { return { editorContent: '', tinymceConfig: { plugins: 'image', toolbar: 'image', images_upload_url: '/api/upload-image', // 图片上传的API URL images_upload_base64: true, // 是否支持base64上传 images_rendition_target_width: 800, // 图片缩略图宽度 }, }; }, methods: { handleImageUpload(file) { this.$fileUpload.upload(file).then((response) => { // 处理上传成功的响应,比如更新富文本中的src属性 const imageUrl = response.url; const imgElement = document.createElement('img'); imgElement.src = imageUrl; this.editorContent = this.editorContent.replace(/\[\[image\]\]/, imgElement.outerHTML); }); }, }, }; </script> ``` 在这个示例中,`tinymceConfig` 配置了TinyMCE的插件、工具栏以及图片上传的URL。当用户在编辑器中添加图片时,`handleImageUpload`方法会被调用,然后通过VueFileUpload上传图片,并更新富文本内容中的图片链接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值