Angular 具有可观测';拦截';类型为';认证检测器';不是>;基类型中的相同属性';HttpInterceptor';
我不明白这个代码出了什么问题 你能帮帮我吗Angular 具有可观测';拦截';类型为';认证检测器';不是>;基类型中的相同属性';HttpInterceptor';,angular,angular-http,rxjs6,Angular,Angular Http,Rxjs6,我不明白这个代码出了什么问题 你能帮帮我吗 import { Injectable } from '@angular/core'; import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http'; import { Observable } from 'rxjs'; import { switchMap, map } from 'rxjs/operators'; imp
import { Injectable } from '@angular/core';
import {
HttpEvent, HttpInterceptor, HttpHandler, HttpRequest
} from '@angular/common/http';
import { Observable } from 'rxjs';
import { switchMap, map } from 'rxjs/operators';
import { AuthenticationSharedStorage } from '../storages/authentication.storage';
import { AuthenticationToken } from '../../models/authentication.model';
@Injectable()
export class AuthenticationInterceptor implements HttpInterceptor {
constructor(private storage: AuthenticationSharedStorage) {}
intercept(req: HttpRequest<any>, next: HttpHandler) {
this.storage.getData()
.pipe(
switchMap((data:AuthenticationToken )=> {
if(data){
const authToken = data.token;
const authReq = req.clone({ setHeaders: { Authorization: authToken } });
return next.handle(authReq);
}
return next.handle(req);
})
);
}
}
从'@angular/core'导入{Injectable};
进口{
HttpEvent、HttpInterceptor、HttpHandler、HttpRequest
}来自“@angular/common/http”;
从“rxjs”导入{Observable};
从“rxjs/operators”导入{switchMap,map};
从“../storages/authentication.storage”导入{AuthenticationSharedStorage};
从“../../models/authentication.model”导入{AuthenticationToken};
@可注射()
导出类AuthenticationInterceptor实现HttpInterceptor{
构造函数(专用存储:AuthenticationSharedStorage){}
拦截(请求:HttpRequest,下一步:HttpHandler){
this.storage.getData()
.烟斗(
switchMap((数据:AuthenticationToken)=>{
如果(数据){
const authToken=data.token;
const authReq=req.clone({setHeaders:{Authorization:authToken}});
返回next.handle(authReq);
}
返回next.handle(req);
})
);
}
}
完全错误是:
类型“AuthenticationInterceptor”中的属性“intercept”不可用
可分配给基类型“HttpInterceptor”中的相同属性。类型
“(req:HttpRequest,next:HttpHandler)=>void”不可赋值
输入“(req:HttpRequest,next:HttpHandler)=>
可观察>'。类型“void”不可分配给类型
“可观察>”。16:3
您需要从
截取
返回一个可观察的
——当前未返回任何内容。假设this.storage.getData()
返回一个(可能是真的,因为它有管道()
),然后在它前面放一个返回
intercept(req: HttpRequest<any>, next: HttpHandler) {
return this.storage.getData()
//...
}
intercept(请求:HttpRequest,下一步:HttpHandler){
返回此.storage.getData()
//...
}
您需要从截取
返回一个可观察的
-当前没有返回任何内容。假设this.storage.getData()
返回一个(可能是真的,因为它有管道()
),然后在它前面放一个返回
intercept(req: HttpRequest<any>, next: HttpHandler) {
return this.storage.getData()
//...
}
intercept(请求:HttpRequest,下一步:HttpHandler){
返回此.storage.getData()
//...
}