从4号角升级到7号角会引起一些问题 [英] upgrading from angular 4 to 7 causing some problems
问题描述
嘿,我已经将我的项目从4号角升级到7号角,并且不推荐使用某些服务和模块.这是我的app.module.ts
hey i have upgraded my project from angular 4 to angular 7 and some of the services, modules are deprecated. this is my app.module.ts
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
BrowserAnimationsModule,
SharedModule,
HttpClientModule,
AppRoutingModule,
NgbModule.forRoot(),
StoreModule.forRoot({})
],
providers: [
SessionTimeoutService,
SpinnerService,
{
provide: HttpClient,
useFactory: httpFactory,
deps: [XHRBackend, RequestOptions, Store, SpinnerService]
},
UtilService,
{ provide: NgbDateParserFormatter, useClass: DateParserFormatter }
],
bootstrap: [AppComponent]
})
export class AppModule {
constructor(private injector: Injector) {
ServiceLocatorService.injector = this.injector;
}
}
现在XHRBackend,RequestOptions现在已被弃用,并给我错误有人可以告诉我如何解决吗?
now XHRBackend, RequestOptions are now deprecated and giving me error can someone tell me how to resolve it?
这是我的Http拦截器文件
and this is my Http interceptor file
@Injectable()
export class InterceptedHttp extends HttpClient {
constructor(
backend: HttpBackend,
defaultOptions: RequestOptions,
private store: Store<any>,
private spinnerService: SpinnerService
) {
super(backend, defaultOptions);
}
request(
url: string | HttpRequest,
options?: RequestOptionsArgs
): Observable<HttpResponse> {
this.showLoader();
return this.tryCatch(super.request(url, options)).finally(() => {
this.hideLoader();
});
}
get(url: string, options?: RequestOptionsArgs): Observable<HttpResponse> {
url = this.updateUrl(url);
return this.tryCatch(super.get(url, this.getRequestOptionArgs(options)));
}
post(
url: string,
body: string,
options?: RequestOptionsArgs
): Observable<HttpResponse> {
url = this.updateUrl(url);
return this.tryCatch(
super.post(url, body, this.getRequestOptionArgs(options))
);
}
put(
url: string,
body: string,
options?: RequestOptionsArgs
): Observable<HttpResponse> {
url = this.updateUrl(url);
return this.tryCatch(
super.put(url, body, this.getRequestOptionArgs(options))
);
}
delete(url: string, options?: RequestOptionsArgs): Observable<HttpResponse> {
url = this.updateUrl(url);
return this.tryCatch(super.delete(url, this.getRequestOptionArgs(options)));
}
patch(
url: string,
body: any,
options?: RequestOptionsArgs
): Observable<HttpResponse> {
url = this.updateUrl(url);
return this.tryCatch(
super.patch(url, body, this.getRequestOptionArgs(options))
);
}
private updateUrl(req: string) {
return environment.origin + req;
}
private getRequestOptionArgs(
options?: RequestOptionsArgs
): RequestOptionsArgs {
if (options == null) {
options = new RequestOptions();
}
if (options.headers == null) {
options.headers = new HttpHeaders();
}
options.headers.append("Content-Type", "application/json");
options.headers.append(
"Authorization",
` Bearer ${sessionStorage.AccessToken}`
);
return options;
}
}
我遇到错误RequestOptions,RequestOptionsArgs,这些都已弃用,现在我遇到了如何解决的错误?
i am getting errors RequestOptions, RequestOptionsArgs, these are deprecated now i am getting errors how to resolve it?
推荐答案
您必须使用@ angular/common/http中的新包喜欢
You have to use new packge from @angular/common/http like
import { HttpClientModule } from "@angular/common/http";
import { HttpClient } from "@angular/common/http";
因此大多数情况下,您将使用HttpHeaders来构造ajax标头,例如params formdata等...
So mostly you will use HttpHeaders to construct your ajax header like params formdata etc...
标题->HttpHeaders
Headers -> HttpHeaders
响应->HttpResponse
Response -> HttpResponse
RequestOptions,RequestOptionsArgs被删除,您必须使用HttpParams
RequestOptions, RequestOptionsArgs are remove and you have to use HttpParams
请在此处
这篇关于从4号角升级到7号角会引起一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!