在拦截器中更新请求的 URL,而不是简单地附加在原始 URL 上。例如:
@Injectable()
export class MyInterceptor implements HttpInterceptor {
constructor(private authService: AuthService) {}
intercept(req: HttpRequest, next: HttpHandler): Observable> {
const authReq = req.clone({
url: this.authService.getNewUrl(req.url) // 更新请求的 URL
});
return next.handle(authReq);
}
}
在 getNewUrl()
方法中,你可以通过一些逻辑来确定应该使用哪个 URL。例如,可以检查当前用户的角色或者使用服务器返回的配置来决定使用哪个 URL。