简体   繁体   English

Angular 属性在“窗口”类型上不存在

[英]Angular Property does not exist on type 'Window'

I have a function to upload, but I have the following errors:我有一个 function 要上传,但出现以下错误:

Property 'File' does not exist on type 'Window'.类型“窗口”上不存在属性“文件”。 Property 'FileList' does not exist on type 'Window'.类型“窗口”上不存在属性“文件列表”。 Property 'FileReader' does not exist on type 'Window'. “Window”类型上不存在属性“FileReader”。 Property 'result' does not exist on type 'EventTarget'. “EventTarget”类型上不存在属性“结果”。

MY Component我的组件

import { Component, OnInit } from '@angular/core';
import * as $ from 'jquery';
import { UploadService } from './upload.service';
import 'jquery-ui-bundle';


@Component({
  selector: 'app-upload',
  templateUrl: './upload.component.html',
  styleUrls: ['./upload.component.css']
})
export class UploadComponent implements OnInit {
  categories: any;

  constructor( private uploadService: UploadService) { }

  ngOnInit() {
    $(document).ready(function() {
      document.getElementById('pro-image').addEventListener('change', readImage, false);

      $( ".preview-images-zone" ).sortable();

      $(document).on('click', '.image-cancel', function() {
          let no = $(this).data('no');
          $(".preview-image.preview-show-"+no).remove();
      });
  });

  var num = 1;
  function readImage(event) {
      if (window.File && window.FileList && window.FileReader) {
          var files = event.target.files; //FileList object
          var output = $(".preview-images-zone");

          for (let i = 0; i < files.length; i++) {
              var file = files[i];
              if (!file.type.match('image')) continue;

              var picReader = new FileReader();

              picReader.addEventListener('load', function (event) {
                  var picFile = event.target;
                  var html =  '<div class="preview-image preview-show-' + num + '">' +
                              '<div class="image-cancel" data-no="' + num + '">x</div>' +
                              '<div class="image-zone"><img id="pro-img-' + num + '" src="' + picFile.result + '"></div>' + '</div>';

                  $(".preview-image.preview-show-" + num).remove();
                  output.prepend(html);
                  num = num + 1;
              });

              picReader.readAsDataURL(file);
          }
          $("#pro-image").val('');
      } else {
          console.log('Browser not support');
      }
  }
  }
}

Since File is not an existing property of window.由于 File 不是 window 的现有属性。 You will need to cast window as any or use object['property'] notation您需要将 window 转换为 any 或使用 object['property'] 表示法

if ((window as any).File && (window as any).FileList && (window as any).FileReader) or if (window['File'] && window['FileList'] && window['FileReader'] if ((window as any).File && (window as any).FileList && (window as any).FileReader) 或 if (window['File'] && window['FileList'] && window['FileReader']

instead of attaching change listener in component you can do this in html.您可以在 html 中执行此操作,而不是在组件中附加更改侦听器。 This is the better way of doing this.这是执行此操作的更好方法。 Try to avoid jQuery as much as possible in the component to make the code look cleaner.尽量在组件中避免 jQuery,使代码看起来更干净。

<input type="file" (change)="fileChangeListener($event)">

in component在组件中

fileChangeListener($event) {
    const file: File = $event.target.files[0];
    const myReader: FileReader = new FileReader();

    myReader.onloadend = (event: any) => {
      this.image = event.target.result;
    };

    myReader.readAsDataURL(file);
  }

Seeing that you use the any type.看到您使用的是any类型。 You can wrap your calls to cast the window type as any .您可以包装您的调用以将window类型转换为any

This:这个:

if (window.File && window.FileList && window.FileReader) { //...

Becomes:变成:

if ((window as any).File && (window as any).FileList && (window as any).FileReader) { //...

I wrote an article on this here .我在这里写了一篇关于这个的文章。 This is typescript complaining that you're trying to access a property on the window that typescript doesn't know about.这是 typescript 抱怨您正在尝试访问 typescript 不知道的 window 上的属性。 This article explains the best way to handle it while remaining type safe.本文解释了在保持类型安全的同时处理它的最佳方法。

You can leverage the same fix for the result.您可以对结果利用相同的修复。

this:这个:

picReader.addEventListener('load', function (event) {
                  var picFile = event.target;

becomes:变成:

picReader.addEventListener('load', function (event) {
                  var picFile = event.target as any;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM