简体   繁体   English

Angular2 - 没有服务错误的提供者

[英]Angular2 - No provider for service error

Running Angular2 framework and I'm getting the mentioned above error message 运行Angular2框架,我得到上面提到的错误消息

Error in ./AppComponent class AppComponent - inline template:2:8 caused by: No provider for ProductService ! ./AppComponent类中的错误AppComponent - 内联模板:2:8导致: 没有ProductService的提供程序

Although the service is defined in the providers array of the app.component.ts file , which is the relevant file for the service to be referenced (see below text). 尽管该服务是在app.component.ts文件的providers数组中定义的,该文件是要引用的服务的相关文件(参见下文)。 The error triggers at core.umd.js file: 错误在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 []
    }
}

product-list.component: 产品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();
    }
}

and the relevant code in the product-list.component.html: 以及product-list.component.html中的相关代码

<table class='table' *ngIf="products && products.length">

确保通过导入在app.module.ts ofcourse中添加提供者ProductService

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

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