在 Angular 个拦截器中获取当前路线
Get current route in Angular Interceptors
我想知道是否有办法在 Angular 中的 HttpInterceptor 中检索当前路由。我正在使用来自不同路线的相同拦截器,但我需要检查拦截器是否正在从特定路线使用。如果是这样,我想在执行之前向后端请求添加一个特定的header。
似乎 Route、Router、ActivatedRoute 和 ActivatedRouteSnapshot 不适用于此。
我目前正在使用 windows.location 作为临时解决方案,但想知道在 HttpRequest 拦截器中执行此操作的正确方法是什么。
我当前的代码:
@Injectable()
export class APIClientInterceptor implements HttpInterceptor {
constructor() {
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// Check if the call is done from the embed route.
if (currentLocation.includes('/embed/')) {
// Get the hash ID.
const sIndex = currentLocation.lastIndexOf('/') + 1;
const sLength = currentLocation.length - sIndex;
const embedHash = currentLocation.substr(sIndex, sLength);
console.log(embedHash);
// Add the header to tell the backend to use client credential flow for this call.
request = request.clone({
setHeaders: {
...
}
});
}
// In all other cases, just pass the original request.
return next.handle(request);
}
}
工作示例:
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
@Injectable()
export class TestInterceptor implements HttpInterceptor {
constructor(private router: Router) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log(this.router.routerState.snapshot);
return next.handle(request);
}
}
最好的方法是通过路由器对象来完成:
constructor(private router: Router) {
console.log(this.router.routerState.snapshot);
}
抱歉回答晚了,但我发现了同样的问题,并且努力寻找最佳解决方案。所以我post把它放在这里供参考
我想知道是否有办法在 Angular 中的 HttpInterceptor 中检索当前路由。我正在使用来自不同路线的相同拦截器,但我需要检查拦截器是否正在从特定路线使用。如果是这样,我想在执行之前向后端请求添加一个特定的header。
似乎 Route、Router、ActivatedRoute 和 ActivatedRouteSnapshot 不适用于此。
我目前正在使用 windows.location 作为临时解决方案,但想知道在 HttpRequest 拦截器中执行此操作的正确方法是什么。
我当前的代码:
@Injectable()
export class APIClientInterceptor implements HttpInterceptor {
constructor() {
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// Check if the call is done from the embed route.
if (currentLocation.includes('/embed/')) {
// Get the hash ID.
const sIndex = currentLocation.lastIndexOf('/') + 1;
const sLength = currentLocation.length - sIndex;
const embedHash = currentLocation.substr(sIndex, sLength);
console.log(embedHash);
// Add the header to tell the backend to use client credential flow for this call.
request = request.clone({
setHeaders: {
...
}
});
}
// In all other cases, just pass the original request.
return next.handle(request);
}
}
工作示例:
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
@Injectable()
export class TestInterceptor implements HttpInterceptor {
constructor(private router: Router) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log(this.router.routerState.snapshot);
return next.handle(request);
}
}
最好的方法是通过路由器对象来完成:
constructor(private router: Router) {
console.log(this.router.routerState.snapshot);
}
抱歉回答晚了,但我发现了同样的问题,并且努力寻找最佳解决方案。所以我post把它放在这里供参考