意外的标记。 Typescrcipt

时间:2017-09-19 08:36:04

标签: angular typescript ng2-file-upload

我正在尝试使用角度4中的ng2-file-upload在item.upload()之后进行回调,但是我收到了一个错误'意外的令牌。期望构造函数,方法,访问者或属性'和'预期的声明或声明'

以下是我的组件和app.module代码。

有没有人节省我的时间

上传组件

                import {  Component } from '@angular/core';
                import * as FileSaver from 'file-saver';
                import * as XLSX from 'xlsx';
                import { FileUploader 
                ,FileItem,ParsedResponseHeaders,FileLikeObject} from 'ng2-file-upload';

                @Component ({  
                   selector: 'my-app',  
                   templateUrl:'./excelUpload.html'

                })  
                export class ExcelUploadComponent  { 

                    private uploader:FileUploader = new FileUploader({url:'http://localhost:5000/upload'});

                    this.uploader.onSuccessItem = (item:FileItem, response:string, status:number, headers:ParsedResponseHeaders) => 
                    { console.log("onSuccessItem " + status, response, item); 
                        if(response) 
                        { //parse your response.
                        } 
                    }
                }

app.module.ts

            import { BrowserModule } from '@angular/platform-browser';
        import { NgModule } from '@angular/core';
        import { FormsModule,ReactiveFormsModule} from '@angular/forms'; 
        import { HttpModule } from '@angular/http';
        import { AppComponent } from './app.component';
        import { RouterModule, Routes } from '@angular/router';
        import {ExcelUploadComponent} from './ExcelUpload/ExcelUpload.Component';
        import {  Component } from '@angular/core';
        import { FileSelectDirective, FileDropDirective } from 'ng2-file-upload';
        import { FileUploadModule } from 'ng2-file-upload/ng2-file-upload';



        @NgModule({
          declarations:[ AppComponent,
             ExcelUploadComponent
            ,FileDropDirective, FileSelectDirective ],
          imports:    [ BrowserModule , FormsModule,HttpModule],
          providers: [],
          bootstrap: [LayoutComponent]

        })
        export class AppModule { }

2 个答案:

答案 0 :(得分:2)

只有班级成员才能直接进入班级。任何需要作为类初始化的一部分执行的代码都应该在构造函数中:

// ...
export class ExcelUploadComponent {

    private uploader: FileUploader = new FileUploader({ url: 'http://localhost:5000/upload' });
    constuctor() {
        this.uploader.onSuccessItem = (item: FileItem, response: string, status: number, headers: ParsedResponseHeaders) => {
            console.log("onSuccessItem " + status, response, item);
            if (response) { //parse your response.
            }
        }
    }

}

答案 1 :(得分:1)

try placing the code within OnInit method 

ngOnInit() {
    //paste the code here
}

if you are yet to implement OnInit add below code to the class file before {
implements ngOnInit
import ngOnInit from angular/core 

e.g
import { Component, OnInit } from '@angular/core';
export class SimpleChatComponent implements OnInit {

ngOnInit() {
    //paste the code here
  }
}