我在使用Angular 4框架的Typescript文件中收到错误。 intelliJ IDE无法识别HttpRequest的任何数据类型。
以下是代码:
import {Component, Injectable, OnInit} from '@angular/core';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/of';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import {ActivatedRoute, Router} from "@angular/router";
import {
HttpClient, HttpErrorResponse, HttpEvent, HttpHandler, HttpHeaders, HttpRequest,
HttpResponse, HttpInterceptor, HttpEventType} from "@angular/common/http";
import * as $ from "jquery";
@Injectable()
export class Service {
public getNames() {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (request.responseType == 'json') {
request = request.clone({ responseType: 'text' });
return next.handle(req).map(response => {
if (response instanceof HttpResponse) {
response = response.clone<any>({ body: JSON.parse(response.body) });
}
return response;
});
}
}
来自以下GitHub网站:https://github.com/angular/angular/issues/18396#issuecomment-349403045
我已经检查过我是否已导入&#39; @ angular / common / http&#39;并且我的Typescript版本是最新的(我有2.6.2版本)。
有谁知道如何解决这个问题? 任何建议都将不胜感激。
谢谢。
答案 0 :(得分:0)
使用拦截器的正确方法是:
import {
Injectable,
} from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor
} from '@angular/common/http';
import {
Observable
} from 'rxjs/Observable';
@Injectable()
export class MyInterceptor implements HttpInterceptor {
constructor() {
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (request.responseType == 'json') {
request = request.clone({ responseType: 'text' });
}
return next.handle(request).map(// your code here)
}
}
找到代码中的差异。