我有一个根组件是AppComponent,我在根组件(ProductService)中注入了一个服务,我试图在其中一个子组件(ProductList)中解析它。
我收到错误没有ProductService的提供商! (ProductListComponent - > ProductService)
以下是Root组件和子组件的代码
import {
Component
}
from 'angular2/core'
import {
ProductListComponent
}
from './products/product-list.component'
import {
ProductService
}
from './products/product.service'
/**
* AppComponent
*/
@
Component({
selector: 'pm-app',
template: `
<div>
<h1>{{PageTitle}}</h1>
<pm-products></pm-products>
</div>
`,
providers: [ProductService],
directives: [ProductListComponent]
})
export class AppComponent {
PageTitle: string = "Acme Product Management";
}
ProductListComponent
@Component({
selector: 'pm-products',
templateUrl: 'app/products/product-list.component.html',
styleUrls: ['app/Products/products-list.component.css'],
pipes: [ProductFilterPipe],
directives: [StarComponent]
})
export class ProductListComponent implements OnInit {
PageTitle: string = "Product List";
imageWidth: number = 50;
imagemargin: number = 2;
showImage: boolean = false;
filterBy: string;
products: IProduct[];
constructor(public _productService:ProductService){
}
}
我对角度2 DI的理解是,如果服务是在root中注入的,它会被注入到它的子组件中。我是否还需要做额外的事情?
答案 0 :(得分:0)
import { ProductService } from './products/product.service'
中确实有ProductListComponent
,对吗?