当我在组件中注入服务时,我得到了上面提到的错误。当我从组件装饰器中删除providers属性时,它就会运行。以下是我的代码片段
ManufactureComponent.ts
import { Component, OnInit } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
import { BaseLayoutComponent } from '../../shared/index';
import { ManufacturerService } from '../index'
@Component({
moduleId: module.id,
templateUrl: 'manufacturers.component.html',
directives: [ROUTER_DIRECTIVES, BaseLayoutComponent]
})
export class ManufacturerComponent implements OnInit {
constructor() {}
ngOnInit() {
}
}
ManufacturerListComponent.ts
import { Component, OnInit, OnDestroy } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
import { ManufacturerService, IManufacturer } from '../index';
import { PageContainerComponent } from '../../shared/index';
@Component({
moduleId: module.id,
templateUrl: 'manufacturer-list.component.html',
directives: [ROUTER_DIRECTIVES, PageContainerComponent],
providers: [ManufacturerService]
})
export class ManufacturerListComponent implements OnInit {
manufacturers: IManufacturer[];
constructor(private manufacturerService: ManufacturerService) { }
ngOnInit() {
this.getManufacturers();
}
getManufacturers() {
this.manufacturerService.all()
.subscribe(response => {this.manufacturers = response;},
(error) => {console.log(error);})
}
}
ManufacturerService.ts
import { Injectable } from '@angular/core';
import { Response } from '@angular/http';
import { Observable } from 'rxjs/observable';
import { AuthHttp } from 'angular2-jwt';
import { Config } from '../../shared/index';
import { IManufacturer } from '../index';
@Injectable()
export class ManufacturerService {
private baseUrl = `${Config.endpoint}/api/manufacturers`;
constructor(private authHttp: AuthHttp) { }
all(): Observable<IManufacturer[]> {
return this.authHttp.get(this.baseUrl)
.map(res => res.json());
}
}
`
我还确保正确导入依赖项。
由于
答案 0 :(得分:1)
您需要在根组件(或AuthHttp
)提供bootstrap()
答案 1 :(得分:0)
进行进一步的调试,我发现它必须与ManufacturerService
以及它如何导入ManufacturerListComponent
。直接导入对我有用。
答案 2 :(得分:0)
如果你的相互依赖的组件位于一个index.ts文件......