注入错误和无提供程序错误时没有提供服务的提供程序

时间:2017-08-03 16:09:30

标签: angular angular2-template angular2-services

error message 我是角色2的新人。即使我在app模块中指定了提供程序,我也没有提供服务注入错误且没有提供程序错误。 代码是                  的 cribs.service.ts

create

app.module.ts

TopicsController

我的文件heirarachy是:c:/User/Desktop/projectname/src/app/service/crib.service.ts C:/User/Desktop/projectname/src/app/app.module.ts

app.component.ts

import { Injectable } from '@angular/core';
import {Http} from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class CribsService {

    constructor(private http: Http) { }


      getAllCribs()
        {
         return this.http.get('data/Cribs.json').map(res => res.json());
         }

   }

婴儿床-listing.component.ts

enter code here
import { AppComponent } from './app.component';
import { CribsListingComponent } from './cribs-listing/cribs-
 listing.component';

 import { CribCardComponent } from './crib-card/crib-card.component';
 import {HttpModule} from '@angular/http';
 import {CribsService} from './service/cribs.service';

  @NgModule({
   declarations: [
   AppComponent,
   CribsListingComponent,
   CribCardComponent,
    ],
   imports: [
       BrowserModule,
       HttpModule
        ],
   providers: [CribsService],
   bootstrap: [AppComponent]
   })
   export class AppModule { }

1 个答案:

答案 0 :(得分:2)

尝试在组件级别提供提供者服务名称,如belo

@Component({
    selector: 'my-compoment',
    template: '<h1>{{ title }}</h1>',
    providers: [CribsService]
})