【问题标题】:Uncaught Error: Can't resolve all parameters for未捕获的错误:无法解析所有参数
【发布时间】:2018-08-22 17:34:48
【问题描述】:

我正在学习 Angular5 以及如何将 useFactorydeps 一起使用。 目前在我的Angular 应用程序中,我有一个组件三个服务。

这是组件

// email.component.ts
import { Component, OnInit, Inject } from '@angular/core';

@Component({
  ...
})
export class EmailComponent implements OnInit {
  private currentEmail: string;
  private companyName: string;

  constructor(@Inject('EMS') private emailService) { }

  ngOnInit() {
    this.currentEmail = this.emailService.emailServiceName;
  }
}

模板是

// email.component.html
<p>Your Current Email Service is : {{ currentEmail }}</p>

这里是app.module.ts

...
import { EmailComponent } from './components/email/email.component';
import { EmailService } from './email-service';
import { HotMail } from './hot-mail';
import { YahooMail } from './yahoo-mail';


function HotMailOrYahooMail(x) {
  if (x.isHotMail) {
    return new HotMail();
  } else {
    return new YahooMail();
  }
}
@NgModule({
  ...
  providers: [
    EmailService,
    {
      provide: 'EMS',
      useFactory: HotMailOrYahooMail,
      deps: [EmailService]
    }
  ],
  ...
})
export class AppModule { }

逻辑EmailComponent应该呈现返回的值来自HotMail服务或YahooMail服务,并且这个逻辑位于 EmailService,即EmailService有一个名为isHotMail的属性,如果它的值为真那么'EMS'应该指向HotMail服务,否则 它应该指向YahooMail 服务。

这里是EmailService

export class EmailService {
    constructor(public isHotMail: boolean) {
        this.isHotMail = true;
    }
}

HotMail服务:

export class HotMail {
  public emailServiceName = 'HotMail by Microsoft';
}

YahooMail服务:

export class YahooMail {
  public emailServiceName = 'YahooMail by Yahoo';
}

但是,在运行ng serve 时,出现以下错误:

Uncaught Error: Can't resolve all parameters for EmailService: (?).
        at syntaxError (compiler.js:485)
        at CompileMetadataResolver._getDependenciesMetadata (compiler.js:15699)
        at CompileMetadataResolver._getTypeMetadata (compiler.js:15534)
        at CompileMetadataResolver._getInjectableMetadata (compiler.js:15514)
        at CompileMetadataResolver.getProviderMetadata (compiler.js:15874)
        at eval (compiler.js:15785)
        at Array.forEach (<anonymous>)
        at CompileMetadataResolver._getProvidersMetadata (compiler.js:15745)
        at CompileMetadataResolver.getNgModuleMetadata (compiler.js:15313)
        at JitCompiler._loadModules (compiler.js:34404)

我在这里错过了什么?

【问题讨论】:

    标签: javascript angular typescript angular5


    【解决方案1】:

    我的错,我应该用这个

    export class EmailService {
        public isHotMail = true;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-18
      • 2018-09-21
      • 2023-03-16
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多