在方法中未定义角注入服务

时间:2019-06-28 09:09:49

标签: angular service

为什么我的SharingCanvasStateService在ngOnInit部分中可见,而在已加载的方法中未定义?尝试执行已加载的方法时,出现“无法读取属性.... odf未定义”

import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {SharingCanvasStateService} from '../../../sharing-canvas-state.service';
import {CanvasState} from '../../../models/canvas-state';

@Component({
  selector: 'app-upload',
  templateUrl: './upload.component.html',
  styleUrls: ['./upload.component.scss']
})
export class UploadComponent implements OnInit {
  @ViewChild('open', { static: true }) openInput: ElementRef<HTMLInputElement>;
  private canvasState: CanvasState = this.sharingCanvasStateService.canvasState;

  constructor(private sharingCanvasStateService: SharingCanvasStateService) { }

  ngOnInit() {
    console.log(this.sharingCanvasStateService.canvasState);
  }

  startRead() {
    // obtain input element through DOM
    const file = this.openInput.nativeElement.files[0];
    if (file) {
      this.getAsText(file);
    }
  }

  getAsText(readFile: any) {
    const reader = new FileReader();
    // Read file into memory as UTF-16
    reader.readAsText(readFile, 'UTF-8');
    // Handle progress, success, and errors
    reader.onload = this.loaded;
  }


  loaded(evt) {
    // Obtain the read file data
    const frames = JSON.parse(evt.target.result);
    console.log(this.sharingCanvasStateService.canvasState);
    this.canvasState.frames = frames;
  }
}

它已添加到app.module提供程序中,所以我不知道为什么它不起作用:

providers: [SharingToolsStateService, SharingCanvasStateService, {
    provide: AuthServiceConfig,
    useFactory: provideConfig
  }],

0 个答案:

没有答案