【问题标题】:ERROR in src/app/app.component.ts(20,27): error TS2345: Argument of type 'string | ArrayBuffer' is not assignable to parameter of type 'string'src/app/app.component.ts(20,27) 中的错误:错误 TS2345:“字符串 | 类型的参数” ArrayBuffer' 不可分配给“字符串”类型的参数
【发布时间】:2020-01-12 21:37:00
【问题描述】:

` src/app/app.component.ts(20,27) 中的错误:错误 TS2345:'string | 类型的参数ArrayBuffer' 不可分配给“字符串”类型的参数。 类型 'ArrayBuffer' 不能分配给类型 'string'。

        import { Component } from '@angular/core';
        declare const fileSelected: any;

         @Component({
       selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
      })
      export class AppComponent {

     binaryStr :any;
     inputObj :any;
      fileSelected(inputObj){

        var fileObj = this.inputObj.files[0];
        var webworkerReader = new FileReader();

        webworkerReader.onload = function(){
       var binaryStr = webworkerReader.result;
       var base64Str = btoa(binaryStr);

        webworkerReader.readAsBinaryString(fileObj);
       };
      }
       onclick(){
       fileSelected(this.inputObj);

      }
   }

为什么会出现此错误?如何解决?

【问题讨论】:

  • 请提供您收到此错误的代码!

标签: angular


【解决方案1】:

这里只使用 angular 变量,而不是创建 var 变量

 var binaryStr = webworkerReader.result; 

this.binaryStr = webworkerReader.result; 

【讨论】:

    【解决方案2】:

    你可以使用FileReader.result

    例如,

    const fileReader: FileReader = new FileReader();
    
    fileReader.onload = (event: Event) => {
       event.target.result; // This is invalid
       fileReader.result; // This is valid
    };
    

    更改:var webworkerReader : FileReader = new FileReader();

    【讨论】:

      猜你喜欢
      • 2018-02-21
      • 1970-01-01
      • 2018-07-28
      • 1970-01-01
      • 2021-08-18
      • 2021-12-20
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多