singleton - 如何使用 aot 在 Angular 5 的组件内部提供服务?

标签 singleton angular5 angular-aot

我遇到了错误

question-images.service.ts(5,2): Error during template compile of 'QuestionImagesService' Only initialized variables and constants can be referenced in decorators because the value of this variable is needed by the template compiler in 'Injectable' 'Injectable' references 'Injectable' 'Injectable' references 'Injectable' 'Injectable' is not initialized at ..\@angular\core\src\di\metadata.ts(138,22)

这是组件

import { Component, OnChanges, OnInit } from '@angular/core';
import { SafeHtml } from '@angular/platform-browser';
import { QuestionImagesService } from '@app/patient/components/assesment/questions/question-images.service';
import { QuestionBase } from '@app/patient/components/assesment/questions/question.base';

@Component({
  selector: 'ez-slider-question',
  templateUrl: './slider-question.component.html',
  styleUrls: ['./slider-question.component.scss'],
  providers: [QuestionImagesService]
})
export class SliderQuestionComponent extends QuestionBase implements OnInit, OnChanges {
  minValue: number;
  maxValue: number;
  value: number;
  smallStep: number;
  imageId = 0;
  images: { [key: number]: SafeHtml } = {};

  constructor(private questionImagesService: QuestionImagesService) { super(); }

  ngOnInit() {
    this.minValue = 1;
    this.smallStep = 1;
    this.maxValue = this.question.answers.length;
    this.value = this.question.userAnswers[0]
      ? Number(this.question.answers.find(answer => answer.id === this.question.userAnswers[0].id).value)
      : this.minValue;
    this.images = this.questionImagesService.startLoadingImages(this.question);
  }

  getNewUserAnswerValues(): string[] {
    return [this.value.toString()];
  }

  onValueChange(value: number) {
    this.imageId = this.question.answers.find(answer => answer.value === value.toString()).id;
  }

这是服务

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core/src/core';
import { SafeHtml, DomSanitizer } from '@angular/platform-browser';

@Injectable()
export class QuestionImagesService {
    private images: { [key: number]: SafeHtml } = {};

    constructor(private httpClient: HttpClient, private domSanitizer: DomSanitizer) { }

    startLoadingImages(question: IQuestion): { [key: number]: SafeHtml } {
        this.images = {};
        question.answers.forEach(answer => {
            if (answer.imageUrl) {
                this.httpClient.get(`http://localhost:54531/${answer.imageUrl}`, { responseType: 'text' })
                    .subscribe(imageHtml => this.images[answer.id] = this.domSanitizer.bypassSecurityTrustHtml(imageHtml));
            }
        });
        return this.images;
    }
}

执行ngserve --aot期间抛出错误。我尝试过谷歌和使用服务工厂,但没有成功。我想以这种方式提供服务,因为我希望每个组件都有一个它的实例。您知道如何实现这一目标吗?

最佳答案

问题是您从 QuestionImagesService 内的错误路径导入 Injectable。将导入更改为:

import { Injectable } from '@angular/core';

关于singleton - 如何使用 aot 在 Angular 5 的组件内部提供服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51742576/

相关文章:

c# - .Net 应用程序范围的变量

java - 对单例 "manager"对象的静态引用的缺点?

C++如何删除在许多类中使用的Singleton对象

angular - 类型 'element' .ts 上不存在属性 'Directive'

Angular 5 从 http 请求中获取正确的 Observable 类型(为什么它总是 Observable<Object>?)

qt - 引用 QML 单例类实例?

html - 如何使用角 Material 表添加多个标题行

angular - 类型 'name' 上不存在属性 'any[]'

css - Angular Cli 和 AOT 问题

angular - AOT - Angular 6 - 指令 SomeComponent,预期 0 个参数,但得到 1 个。对于自制组件