未捕获错误:无法解析所有参数

时间:2018-03-14 09:06:29

标签: javascript angular typescript angular5

我正在学习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的属性,如果其值为true '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)

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

我的坏,我应该用这个

        <CheckBox
            android:id="@+id/cbProductdown_"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ....
            ....
            ....
            android:text="Product"
            />