我收到错误: 尝试使用like:" useClass:ApidataService"或者" ApidataService"在提供者中,也从服务构造函数中删除public。
编译失败。
C:/wamp64/www/angular/project/src/app/app.module.ts (33,69): Argument of type '{ declarations: (typeof AppComponent | typeof ViewComponent | typeof AboutComponent | typeof News...' is not assignable to parameter of type 'NgModule'.
Types of property 'providers' are incompatible.
Type '{ provide: InjectionToken<string>; useValue: string; ApidataService: typeof ApidataService; }[]' is not assignable to type 'Provider[]'.
Type '{ provide: InjectionToken<string>; useValue: string; ApidataService: typeof ApidataService; }' is not assignable to type 'Provider'.
Object literal may only specify known properties, and 'ApidataService' does not exist in type 'Provider'.
我的服务是:
import { Injectable } from '@angular/core';
@Injectable()
export class ApidataService {
public constructor() { }
cars = ['cars','bycycle','van'];
mydata(){
return "this is from apidata";
}
}
和app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { ViewComponent } from './view/view.component';
import { AboutComponent } from './about/about.component';
import { RouterModule, Routes} from '@angular/router';
import { NewsComponent } from './news/news.component';
import {APP_BASE_HREF} from '@angular/common';
import { FormsModule } from '@angular/forms'; //for 2way binding
//SERVICES SAMPLE
import {ApidataService} from './apidata.service';
@NgModule({
declarations: [
AppComponent,
ViewComponent,
AboutComponent,
NewsComponent
],
imports: [
HttpModule,
BrowserModule,
FormsModule,
RouterModule.forRoot([
{path:'About',component:AboutComponent},
{path:'News',component:NewsComponent}
])
],
providers: [
{provide: APP_BASE_HREF, useValue: 'http://localhost:4200/',ApidataService}
],
bootstrap: [AppComponent]
})
export class AppModule { }
答案 0 :(得分:0)
使用useClass
将类添加到提供程序
providers: [
{provide: APP_BASE_HREF, useValue: 'http://localhost:4200/', useClass: ApidataService}
],