【问题标题】:Unable to get property 'add' of undefined or null reference while using angular-material-fileupload使用 angular-material-fileupload 时无法获取未定义或空引用的属性“添加”
【发布时间】:2019-06-30 00:55:17
【问题描述】:

//app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import {ModalModule} from 'angular-custom-modal';
import {MatMenuModule} from '@angular/material/menu';
import {MatListModule} from '@angular/material/list';
import {MatTableModule} from '@angular/material/table';
import { TableDemoComponent } from './table-demo/table-demo.component';
import {HttpClientModule} from '@angular/common/http';
import { FileUploadModalComponent } from './table-demo/file-upload-modal/file-upload-modal.component';
import {FormsModule} from '@angular/forms';
import { TestModalComponent } from './test-modal/test-modal.component';
import {MatDialogModule} from'@angular/material';
import {MatButtonModule} from '@angular/material/button';
import { MatFileUploadModule } from 'angular-material-fileupload';
import { MyDialogComponent } from './my-dialog/my-dialog.component';

@NgModule({
  declarations: [
    AppComponent,
    TableDemoComponent,
    FileUploadModalComponent,
    TestModalComponent,
    MyDialogComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    MatMenuModule,
    MatListModule,
    MatTableModule,
    HttpClientModule,
    ModalModule,
    FormsModule,
    MatDialogModule,
    MatButtonModule,
    MatFileUploadModule
  ],
  entryComponents:[
    MyDialogComponent
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

//app.component.html

<div [fileUploadInputFor]="fileUploadQueue" class="upload-drop-zone"></div>

//app.component.ts

   import { Component, OnInit, ViewChild } from '@angular/core';

@Component({
  selector: 'app-table-demo',
  templateUrl: './table-demo.component.html',
  styleUrls: ['./table-demo.component.scss']
})
export class TableDemoComponent implements OnInit {

  constructor() { }

  ngOnInit() {

  }


  @HostListener('drop', [ '$event' ])
  public onDrop(event: any): any {
    alert("hi");
  }
}

//app.component.css

.upload-drop-zone {
  min-height: 400px;
  min-width: 400px;
  display: table;
  width: 100%;
  background-color: #eee;
  border: dotted 1px #aaa;
}

错误是:

错误类型错误:无法读取 null 的属性“添加” 在 FileUploadInputFor.push../node_modules/angular-material-fileupload/matFileUpload.esm.js.FileUploadInputFor.onDrop (matFileUpload.esm.js:7622)

文件matFileUpload.esm.js属于angular-material-fileupoad的源代码

我正在使用此链接上的此链接code 实现此代码我正在尝试实现拖放文件上传功能的第二个代码。

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    如果你上面贴的代码是实际的代码,那么从你给here的参考链接看,你似乎没有正确实现代码。

    请查看代码here,您会看到输入时还需要&lt;mat-file-upload-queue&gt;...&lt;/mat-file-upload-queue&gt;,如下所示:

    <div [fileUploadInputFor]="fileUploadQueue" class="upload-drop-zone">
        Just drag and drop files here
    </div>
    
    <!-- You forgot below part --> 
    <div style="width: 500px">
        <mat-file-upload-queue #fileUploadQueue
            [fileAlias]="'file'"
            [httpUrl]="'http://localhost:8180/jax-rs-jersey-application-sample'" multiple>
    
            <mat-file-upload [file]="file" [id]="i" *ngFor="let file of fileUploadQueue.files; let i = index"></mat-file-upload>
        </mat-file-upload-queue>
    </div>
    

    【讨论】:

    • 我只需要上传一个文件,这就是我不实现该代码的原因
    • 我猜你需要更多地理解代码。没有mat-file-upload-queue,您的代码将无法工作。您特别提到了mat-file-upload-queue。在你的第一个div 中注意[fileUploadInputFor]="fileUploadQueue",这意味着你用#fileUploadQueue 引用你的mat-file-upload-queue
    • 我不想实现 mat-file-upload-queue 功能,这就是我不实现它的原因。如果我想在没有 mat-file-upload-queue 的情况下上传单个文件,我该怎么办?
    • 您使用的 angular-material-fileupload 包本身需要 MatFileUploadModule。如果您不想使用 MatFileUploadModule,请另找一个包。
    • 我们有更好的 MatFileUploadModule 实现吗,请告诉我,
    猜你喜欢
    • 1970-01-01
    • 2019-05-13
    • 2017-07-05
    • 2014-08-01
    • 2020-04-28
    • 2016-08-16
    • 2017-05-18
    • 1970-01-01
    • 2014-08-29
    相关资源
    最近更新 更多