我通过Visual Studio模板构建了一个Angular应用程序。
给出了以下结构:
所以在person-data.service.ts中我想使用auth-http.service.ts。
人-data.service.ts
import { Person } from '../models/person'
import { Configuration } from '../constants/global.constants';
import { Injectable, Inject } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
import { AuthHttpService } from '../services/auth-http.service';
@Injectable()
export class PersonService {
constructor(private http: Http, @Inject(AuthHttpService)private authHttp: AuthHttpService) {
this.actionUrl = Configuration.API_SERVER + 'api/person/';
this.headers = new Headers();
this.headers.append('Content-Type', 'application/json');
this.headers.append('Accept', 'application/json');
}
public GetAll = (): Observable<Person[]> => {
return this.authHttp.get(this.actionUrl).map((response: Response) => <Person[]>response.json());
}
}
AUTH-http.service.ts
import { Injectable, Inject } from '@angular/core';
import { Http, Response, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { AuthService } from './auth.service';
@Injectable()
export class AuthHttpService {
constructor(private http: Http, @Inject(AuthService) private authService: AuthService) {
}
get(url: string, options?: RequestOptions): Observable<Response> {
console.log("AuthHttpService Get:" + url);
if (options) {
options = this.authService._setRequestOptions(options);
} else {
options = this.authService._setRequestOptions();
}
return this.http.get(url, options);
}
}
app.module.shared.ts
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { PersonService } from './services/person-data.service'
import { Configuration } from './constants/global.constants'
import { AuthService } from './services/auth.service'
import { AuthHttpService } from './services/auth-http.service'
import { AppComponent } from './components/app/app.component'
export const sharedConfig: NgModule = {
bootstrap: [AppComponent],
declarations: [
AppComponent
],
providers: [
AuthHttpService,
Configuration,
PersonService,
AuthService
],
imports: [
RouterModule.forRoot([
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: '**', redirectTo: 'home' }
])
]
};
app.module.client.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { sharedConfig } from './app.module.shared';
@NgModule({
bootstrap: sharedConfig.bootstrap,
declarations: sharedConfig.declarations,
imports: [
BrowserModule,
FormsModule,
HttpModule,
BrowserAnimationsModule,
...sharedConfig.imports
],
providers: [
{ provide: 'ORIGIN_URL', useValue: location.origin }
]
})
export class AppModule {
}
当我运行应用程序时,我收到以下错误。
处理请求时发生未处理的异常。 例外:调用节点模块失败,错误:错误:没有提供者 for AuthHttpService!
我错过了什么?
答案 0 :(得分:1)
尝试从构造函数中删除inject装饰器,因为它们不需要。
然后,正如错误所示,然后导入模块的提供程序内部,例如:
providers: [
AuthHttpService,
// Other services here,
{ provide: 'ORIGIN_URL', useValue: location.origin }
]
答案 1 :(得分:0)
我想你忘记了
在sharedConfig.providers
app.module.client.ts
<强> app.module.client.ts 强>
...
providers: [
...sharedConfig.providers,
{ provide: 'ORIGIN_URL', useValue: location.origin }
]
...