我知道我可以在不使用拦截器的情况下做下一件事,但是我试图弄清楚在每个httpRequest上添加了额外的参数。我一直在阅读有关(https://github.com/angular/angular/issues/18812)的问题,该问题重复了参数。我尝试了提供的解决方法,但是没有用。
一次我看到一种简单的方式来做类似const newReq = req.url +'api_key'的操作(请不要接受它的字面意思,它不能那样工作),但我不记得了。任何人都可以让我知道如何获得预期的行为?
预期行为-> https://api.xxx.com/xxx&api_key='api_key '
当前行为-> https://api.xxx.com/xxx&api_key=undefined&api_key='whatever '
@Injectable()
export class ApiKeyInterceptorService implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
let newParams = new HttpParams({fromString: req.params.toString()});
newParams = newParams.append('api_key', '123123123asdasdasd');
const requestClone = req.clone({
params: newParams
});
return next.handle(requestClone);
}
}
设置应该可以解决问题:
newParams = newParams.set('api_key', '123123123asdasdasd');
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句