ElementUI 组件 upload 上传文件时携带 token

  1. data 中定义 token 参数
    data() {
      return {
        token: "",
        fileList: []
    },
    
  2. 本地存储中获取 token
    mounted() {
      this.token = window.sessionStorage.getItem("token");
    },
    
  3. computed 计算属性中计算 headers
    computed: {
      config() {
        return { token: this.token };
      },
    },
    
  4. upload上传组件中配置 headers
    <el-upload
      drag
      :action="this.$baseUrl + '/upload/add'"
      :headers="config"
      multiple
      :file-list="fileList"
      :on-success="handleSuccess"
    >
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
    </el-upload>
    
Logo

前往低代码交流专区

更多推荐