简体   繁体   English

为什么Angular2不能注入我的服务?

[英]Why can't Angular2 inject my service?

I was in the process of writing a plnkr to test some routing issues when suddenly this error message popped up and wouldn't go away no matter what I changed in my code: 我正在编写一个plnkr来测试一些路由问题突然出现这个错误消息,无论我在代码中改变了什么都不会消失:

EXCEPTION: Error: Uncaught (in promise): Can't resolve all parameters for EmployeeListComponent: (?). EXCEPTION:错误:未捕获(在承诺中):无法解析EmployeeListComponent的所有参数:(?)。

Here's the relevant portion of the code: 这是代码的相关部分:

export class EmployeeListComponent {
  employees: Employee[];
  constructor(private _employeeService: EmployeeService) {
    _employeeService.getAll().subscribe(employees =>
      this.employees = employees);
  }
}

So it seems Angular is unable to resolve EmployeeService , right? 所以似乎Angular无法解析EmployeeService ,对吧? I commented it out in the above snippet and sure enough, no errors. 我在上面的片段中对此进行了评论,当然,没有错误。

Did I forget to register it within main.ts or app.component.ts ? 我忘了在main.tsapp.component.ts注册吗? Nope, 不,

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

import { 
    ROUTER_DIRECTIVES
} from '@angular/router';

import {
  EmployeeService
} from './employee.service';


@Component({
  selector: 'plk-app',
  template: '<router-outlet></router-outlet>',
  directives: [ 
    ROUTER_DIRECTIVES
  ],
  providers: [
    EmployeeService
  ]
})
export class AppComponent {

}

Should I have registered it in main.ts ? 我应该在main.ts注册吗? Well, I had when that error first cropped up and I then moved it to app.component.ts to check if that would fix it, it didn't. 好吧,我在第一次出现错误时就把它移到了app.component.ts来检查是否会修复它,但事实并非如此。

At the moment I can't see the forest for the trees and Angular's error messages aren't really helping (they never have been). 目前我无法看到森林中的树木,而Angular的错误消息并没有真正帮助(它们从来没有)。 Is there anyone here who can spot the issue? 这里有人能发现这个问题吗? The plnkr can be found here . plnkr可以在这里找到。

After visiting on few questions I came to know that this problem occurs because of absence of TypeScript compiler. 在访问几个问题后,我发现由于缺少TypeScript编译器而出现此问题。 If you are not using typescript compiler then you need to explicitly inject service by using ' Inject ' from ' @angular/core '. 如果您没有使用typescript编译器,那么您需要使用' @angular/core '中的' Inject '来显式注入服务。 I have tried it in plunker provide by you and its working. 我已经尝试过你提供的plunker及其工作。

In your case you need to make changes in your employee-list.component.ts . 在您的情况下,您需要在employee-list.component.ts进行更改。 Import Inject from @angular/core and in constructor explicitly inject your service like: @angular/core导入Inject并在构造函数中显式注入您的服务,如:

constructor(@Inject(EmployeeService) private employeeService: EmployeeService){}

Feel free for any question and accept my answer if you found the solution helpful to you. 如果您发现该解决方案对您有帮助,请随意提出任何问题并接受我的回答。

Here is working plunker 这是工作的plunker

First step: 第一步:

system.js.config system.js.config

var config = {
    // DEMO ONLY! REAL CODE SHOULD NOT TRANSPILE IN THE BROWSER
    transpiler: 'ts',
    typescriptOptions: {
      tsconfig: true,
      emitDecoratorMetadata: true <== add this line
    },

Second step: 第二步:

employee.model.ts now looks like: employee.model.ts现在看起来像:

export class Employee {
  id: number;
  name: string;
}

Third step: 第三步:

Remove async pipe from template of EmployeeListComponent . EmployeeListComponent模板中删除async管道。 It should be: 它应该是:

<li *ngFor="let employee of employees">
  ...
</li>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM