javascript - Angular 类型 'split' 上不存在属性 'ArrayBuffe

我正在尝试使用 Filereader 读取 CSV 文件,并希望将内容转换为数组。我能够正确获取 CSV 文件,但每当我想将 CSV 文件的内容转换为数组时,我都会遇到此错误。


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


    ERROR in src/app/app.component.ts(31,10): error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
        src/app/app.component.ts(31,21): error TS2339: Property 'split' does not exist on type 'string | ArrayBuffer'.
          Property 'split' does not exist on type 'ArrayBuffer'.

这是我的 app.component.html 文件:

    <nav class="navbar navbar-light bg-light">
      <a class="navbar-brand" href="#">
        <img src="/docs/4.0/assets/brand/bootstrap-solid.svg" width="30" height="30" class="d-inline-block align-top"
          alt="">
        Floor Plan
      </a>
    </nav>

    <div class="card m-5">
      <div class="row row-5">
        <div class="col-4">
          <div class="input-group">
            <div class="custom-file">
              <input type="file" class="custom-file-input" id="inputGroupFile04" (change)="upload($event.target)">
              <label class="custom-file-label" for="inputGroupFile04">Choose file</label>
            </div>
            <div class="input-group-append">
              <button class="btn btn-outline-secondary" type="button" class="btn btn-primary btn-sm">Upload </button>
            </div>
          </div>
        </div>

        <div class="col-8 border border-primary" >
          {{csvContent}}

        </div>

      </div>
    </div>

这是我的 app.component.ts 文件:

    export class AppComponent {
      fileToUpload: File = null;
      title = 'floor-plan';
      csvContent: string[] = []



      upload(input: HTMLInputElement) {

        const files = input.files;
        var content = this.csvContent;

        if (files && files.length) {

          const fileToRead = files[0];

          const fileReader = new FileReader();
          fileReader.onload = (event) => {
            this.csvContent = (event.target as FileReader).result.split('\n').map((data) => {
              return data.split(',')
            })

          }

          fileReader.readAsText(fileToRead, "UTF-8");
        }

      }
     }

最佳答案

FileReader result 属性的类型对于 TypeScript 来说很棘手,因为它取决于您在代码中其他地方调用的方法。

在您的情况下,您正在调用 readAsText所以你知道 result 包含一个字符串,而不是一个 ArrayBuffer,但 TypeScript 不知道。

您需要类型保护或类型断言。例如,使用类型保护:

fileReader.onload = (event) => {
  const result = fileReader.result;
  if (typeof result !== "string') {
    throw new Error("Unexpected result from FileReader");
  }
  this.csvContent = result.split('\n').map((data) => {
    return data.split(',')
  })
};

或者使用类型断言:

fileReader.onload = (event) => {
  this.csvContent = (fileReader.result as string).split('\n').map((data) => {
    return data.split(',')
  })
};

在上面的两个例子中,我使用了 fileReader 而不是 event.target 因为 onload 处理程序关闭了它。

关于javascript - Angular 类型 'split' 上不存在属性 'ArrayBuffer',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60043704/

相关文章:

r - 如何使用 token 在 R 中调用 API

docker - 我想在正在运行的 docker 容器中使用 docker sdk,并想在正在运行的

reactjs - 使用 react-sortable-hoc 拖动时丢失 tr 元素的样式

awk - 将 'u' 转换为 't' 而无需更改 FASTA 中的 header

reactjs - SWR vs Isomorphic-unfetch 用于数据获取 Next.Js

javascript - 数组中的对象 数组中的对象 数组中的对象 JavaScript - 搜索

c++ - 错误 : cannot add a default template argument

javascript - 使用 axios 使用 get 方法发送嵌套的 json 数据

r - 提取第n个字符直到R中的字符串结尾

kubernetes - 集群上的事件与 kustomize 之间的区别