vue 插件 组件_Vue插件,具有用于选择图像的简单组件

vue 插件 组件

Vue图像选择器 (vue-image-chooser)

This is vue plugin with simple component for choosing image. It supports easy implementation for image upload to the backend.

这是vue插件,具有用于选择图片的简单组件。 它支持将图片上传到后端的简单实现。

安装 (Installation)

npm i vue-image-chooser --save

Registring plugin:

注册插件:

import VueImageChooser from 'vue-image-chooser'

Vue.use(VueImageChooser)

This will make vue-image-chooser component globaly available. Bellow is example how to use it with axios library

这将使vue-image-chooser组件全局可用。 贝娄是如何与axios库一起使用的示例

<div class="MyContent">
  <h1>Click on chooser to choose image</h1>
  <div style="width:400px;margin: 0 auto">
    <vue-image-chooser name="image-chooser" @change="uploadFile" :progress="progress" :error="error"/>
  </div>
</div>
export default {
  data() {
    return {
      progress: null,
      error: null,
    }
  },
  methods: {
    uploadFile(file) {
      this.progres = 0;
      let formData = new FormData();
      formData.append('image', file);
      var config = {
        onUploadProgress: progressEvent => {
          var percentCompleted = Math.round(
            (progressEvent.loaded * 100) / progressEvent.total
          )
          this.progress = percentCompleted
        }
      }
      try {
        const { data } = await axios.post(
          '/api/route/to/upload/image',
          formData,
          config
        )
      } catch (e) {
        this.error = 'Error has occured'
      }
    }
  }
}

用法 (Usage)

The component takes four props:

该组件具有四个道具:

  • name (String): Required prop, must be unique (you can consider it like id)

    name (字符串):必需的道具,必须唯一(您可以像id一样考虑)

  • progress (Number): Number between 0-100. If it is null or 100 component consider uploading process finished.

    progress (Number):介于0-1000-100之间的数字。 如果为null100组件,请考虑完成上传过程。

  • error (String): Error message in case that server responded with error

    error (字符串):如果服务器响应错误,则会显示错误消息

  • height (String): Default is 350px

    height (字符串):默认为350px

The component emits change event with single file as data, when it recieve an image, after it can be uploaded as described above)

组件收到图像后,会如上所述将其上传,并以单个文件作为数据发出change事件。

翻译自: https://vuejsexamples.com/vue-plugin-with-simple-component-for-choosing-image/

vue 插件 组件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值