vue img图片 axios请求携带参数、系统头Header

最近需求要求系统下载图片携带token系统头验证权限,一般的img用src下载是get请求且不经过axios全局配置,会携带不上token。所以不能用img,只能自己写一个图片组件替代img

自己建一个组件 authImg

核心

<template>
  <img ref="img" />
</template>
<script>
export default {
  name: 'authImg',
  props: {
    authSrc: {
      type: String,
      required: false,
      default: ''
    }
  },
  mounted() {
    this.getImg()
  },
  // 方法
  methods: {
    getImg() {
      // 获取需携带的token,看自己系统怎么获取的
      let token = sessionStorage.getItem('token')
      Object.defineProperty(Image.prototype, 'authsrc', {
        writable: true,
        enumerable: true,
        configurable: true
      })
      let img = this.$refs.img
      let request = new XMLHttpRequest()
      request.responseType = 'blob'
      request.open('get', this.authSrc, true)
      // 携带请求头
      request.setRequestHeader('Authorization', token)
      request.onreadystatechange = e => {
        if (request.readyState === XMLHttpRequest.DONE && request.status === 200) {
          img.src = URL.createObjectURL(request.response)
          img.onload = () => {
            URL.revokeObjectURL(img.src)
          }
        }
      }
      request.send(null)
    }
  },
  // 监听
  watch: {
    // vue2这里表示父组件是对象属性,子组件不会主动调用,需要深度监听一下调用
    authSrc: {
      deep: true,
      handler(n, o) {
        if (n) {
          this.getImg()
        }
      }
    }
  }
}
</script>

父组件

 

引入authImg,
注册组件
直接替换img
// 原来的
<!--                <img  :src="a.b" />-->
// 现在的;这里的情况,子组件的watch就有用了
<AuthImg  :authSrc="a.b" />

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
vue-quill-editor是一个基于Quill.js的富文本编辑器,它支持上传图片,以下是上传图片的步骤: 1. 安装依赖 ``` npm install quill-image-resize-module --save npm install quill --save npm install vue-quill-editor --save ``` 2. 引入依赖 ```javascript import { quillEditor } from 'vue-quill-editor' import Quill from 'quill' import ImageResize from 'quill-image-resize-module' Quill.register('modules/imageResize', ImageResize) ``` 3. 配置vue-quill-editor ```javascript <template> <quill-editor v-model="content" :options="editorOption"></quill-editor> </template> <script> export default { data () { return { content: '', editorOption: { modules: { imageResize: { displaySize: true }, toolbar: [ ['bold', 'italic', 'underline', 'strike'], [{ 'header': 1 }, { 'header': 2 }], [{ 'color': [] }, { 'background': [] }], [{ 'list': 'ordered' }, { 'list': 'bullet' }], ['image', 'code-block', 'link'] ] }, placeholder: '请输入内容' } } } } </script> ``` 4. 添加上传图片功能 ```javascript <template> <quill-editor v-model="content" :options="editorOption" ref="myQuillEditor"></quill-editor> <input type="file" @change="uploadImage" ref="fileInput" style="display:none"> </template> <script> export default { data () { return { content: '', editorOption: { modules: { imageResize: { displaySize: true }, toolbar: [ ['bold', 'italic', 'underline', 'strike'], [{ 'header': 1 }, { 'header': 2 }], [{ 'color': [] }, { 'background': [] }], [{ 'list': 'ordered' }, { 'list': 'bullet' }], ['image', 'code-block', 'link'] ] }, placeholder: '请输入内容' }, imageSrc: '' } }, methods: { uploadImage() { const file = this.$refs.fileInput.files[0] const formData = new FormData() formData.append('image', file) axios.post('/api/uploadImage', formData).then(res => { if (res.data.status === 'success') { const range = this.$refs.myQuillEditor.quill.getSelection() const imgSrc = res.data.data.imageSrc this.$refs.myQuillEditor.quill.insertEmbed(range.index, 'image', imgSrc) this.$refs.fileInput.value = '' } }) } } } </script> ``` 5. 其中,`uploadImage`方法用于上传图片,并将图片插入到编辑器中。 注意:要将`image`添加到`toolbar`中,以便能够在编辑器中添加图片。 ```javascript ['image', 'code-block', 'link'] ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值