我试图创建一个通用服务来调用我的API,并且构建给了我该错误:
无法解析/ng-app/src/app/services/data.service.ts中的DataService的所有参数:(?,[object Object])。
我知道我不是第一个获得它的人,但是我发现的所有内容都无济于事。
这是我尝试过的:
我不知道我还能尝试解决什么...
这是一些代码:
DataService:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { catchError } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { AppError } from '../common/validators/app-error';
import { NotFoundError } from '../common/not-found-error';
import { BadRequest } from './../common/bad-request-error';
@Injectable({
providedIn: 'root'
})
export class DataService {
constructor(private url: string, public httpClient: HttpClient) { }
getAll(){
return this.httpClient.get(this.url).pipe(catchError(this.handleError));
}
getOne(ressource){
return this.httpClient.get(this.url + '/' + ressource).pipe(catchError(this.handleError));
}
create(ressource){
return this.httpClient.post(this.url, JSON.stringify(ressource)).pipe(catchError(this.handleError));
}
update(ressource,oldRessource){
return this.httpClient.put(this.url + '/' + oldRessource, JSON.stringify(ressource)).pipe(catchError(this.handleError));
}
delete(ressource){
return this.httpClient.delete(this.url + '/' + ressource).pipe(catchError(this.handleError));
}
public handleError(err: Response){
if (err.status == 400 ){
return Observable.throw(new BadRequest(err));
}
if (err.status == 404) {
return Observable.throw(new NotFoundError());
}
// Return error by default if no specific error
return Observable.throw(new AppError(err));
}
}
从DataService扩展的服务
import { HttpClient } from '@angular/common/http';
import { DataService } from './data.service';
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class CategorieService extends DataService {
constructor(httpClient: HttpClient) {
super('http://localhost:8081/categories' ,httpClient);
}
}
更新:
我知道必须使用ToketInjection模式来解决我的问题。我还使用环境类来避免必须在不同位置设置基本URL。
但是我仍然不知道如何为从DataService扩展的每个服务更改DataService中的URL。
这是一些代码:
App.module.ts:
...
import { CategorieService } from './services/categorie.service';
import { DataService } from './services/data.service';
import { environment } from 'src/environments/environment';
export function getBaseUrl(): string {
return environment.API_BASE_URL;
}
@NgModule({
declarations: [
...
],
imports: [
...
])
],
providers: [
...
DataService,
CategorieService,
{ provide: 'API_BASE_URL', useFactory: getBaseUrl },
],
bootstrap: [AppComponent]
})
export class AppModule { }
Environement.ts
export const environment = {
production: false,
API_BASE_URL: "http://localhost:8081/",
};
Data.service.ts
import { Injectable, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { catchError } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { AppError } from '../common/validators/app-error';
import { NotFoundError } from '../common/not-found-error';
import { BadRequest } from './../common/bad-request-error';
@Injectable({
providedIn: 'root'
})
export class DataService {
//constructor(private url: string, public httpClient: HttpClient) { }
constructor(@Inject('API_BASE_URL') private url, public httpClient: HttpClient) { }
getAll(){
return this.httpClient.get(this.url).pipe(catchError(this.handleError));
}
getOne(ressource){
return this.httpClient.get(this.url + '/' + ressource).pipe(catchError(this.handleError));
}
create(ressource){
return this.httpClient.post(this.url, JSON.stringify(ressource)).pipe(catchError(this.handleError));
}
update(ressource,oldRessource){
return this.httpClient.put(this.url + '/' + oldRessource, JSON.stringify(ressource)).pipe(catchError(this.handleError));
}
delete(ressource){
return this.httpClient.delete(this.url + '/' + ressource).pipe(catchError(this.handleError));
}
public handleError(err: Response){
if (err.status == 400 ){
return Observable.throw(new BadRequest(err));
}
if (err.status == 404) {
return Observable.throw(new NotFoundError());
}
// Return error by default if no specific error
return Observable.throw(new AppError(err));
}
}
我想像在尝试到达API的特定部分之前一样,在基本API URL中添加一些参数,但是我不知道如何使用TokenInjection做到这一点。
答案 0 :(得分:1)
您确实不想在构造函数中放入字符串。您可以执行类似injection token的操作,也可以将该字符串作为const放入环境文件中,然后像提供here一样将环境作为提供程序导入。