运行Angular2框架,我收到上面提到的错误消息
./AppComponent类中的错误AppComponent - 内联模板:2:8导致:没有ProductService的提供者!
虽然服务是在app.component.ts文件的providers数组中定义的,但它是要引用的服务的相关文件(参见下文)。错误在core.umd.js文件中触发:
import { ProductService } from './products/product.service';
@Component({
selector: 'pm-app',
template: ``,
providers:[ ProductService ]
})
product.Service code:
import {Injectable} from '@angular/core';
import { IProduct } from './product';
@Injectable()
export class ProductService
{
getProducts(): IProduct[]
{
return []
}
}
产品list.component:
@Component ({
selector: 'pm-products',
moduleId: module.id,
templateUrl: 'product-list.Component.html',
styleUrls: ['product-list.Component.css']
})
export class ProductListComponent implements OnInit
{
products: IProduct[];
constructor(private _productService: ProductService){}
ngOnInit(): void{
this.products = this._productService.getProducts();
}
}
以及 product-list.component.html:
中的相关代码<table class='table' *ngIf="products && products.length">
答案 0 :(得分:0)
确保使用导入
在app.module.ts ofcourse中添加提供商ProductService