我用symfony / angular5开发应用程序。 我想在requestInterceptor上获取当前激活的组件。 这是我的拦截器
import { Injectable, Injector } from '@angular/core';
import {
HttpEvent,
HttpInterceptor,
HttpHandler,
HttpRequest,
HttpErrorResponse
} from '@angular/common/http';
import { environment } from '../../environments/environment';
import { Observable } from 'rxjs/Observable';
import { TokenService } from './token.service';
import { AuthenticationService } from './authentication.service';
import { retry } from 'rxjs/operators';
import { ActivatedRoute, Router } from '@angular/router';
@Injectable()
export class RequestInterceptor implements HttpInterceptor {
constructor(
private tokenService: TokenService,
private injector: Injector
) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
let loggedUser: any = this.getLoggedUser();
let options: any = {};
if (this.isApiRequest(req.url)) {
options.setParams = {
selectedOrganization: loggedUser.selectedOrganizationId,
selectedOrganizationType: loggedUser.selectedOrganizationTypeId
};
options.setHeaders = { Authorization: `Bearer ${this.tokenService.getAccessToken()}` };
console.log('secured');
return this.execute(req, next, options)
}
else {
console.log('unsecured');
return this.execute(req, next, null).retry(1)
}
}
protected isApiRequest(url) {
return url.startsWith(environment.server_api);
}
private getLoggedUser() {
return this.injector.get(AuthenticationService).getUserFromStorage();
}
protected execute(req, next, options = null): Observable<HttpEvent<any>> {
const authReq = (options !== null) ? req.clone(options) : req.clone();
console.log(authReq.url);
return next.handle(authReq)
.catch((err: any, caught) => {
if (err instanceof HttpErrorResponse) {
if (err.status === 403) {
console.info('err.error =', err.error, ';');
}
if (err.status === 401) {
return this.injector.get(AuthenticationService).refreshToken().subscribe(
(res) => {
const authReq = req.clone(options.setHeaders = { Authorization: `Bearer ${this.tokenService.getAccessToken()}` });
// here i have to re-init current component
}
)
}
return Observable.throw(err);
}
});
}
}
这是我的组件
import { Component, ViewChild } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { MatSelect } from '@angular/material';
import { Router } from '@angular/router';
import { AuthenticationService } from '../../../services/authentication.service';
import { DataService } from '../../../services/data.service';
import { Constants } from '../../../constant/constants';
import { CoreUserAdditional } from '../../../models/core-user-additional.model';
import { CoreOrganization } from '../../../models/all';
import { Functions } from '../../../common/helpers/functions';
import { PasswordValidation } from "../../../common/validators/Password-validation";
@Component({
selector: 'app-create-user-additional',
templateUrl: './create-user-additional.component.html'
})
export class CreateUserAdditionalComponent {
spinnerstate: boolean = false;
coreUserAdditional = new CoreUserAdditional();
form: FormGroup
coreOrganizations: any[] = []
coreAgencies: any[] = []
organizationTypes: any[] = []
organizationNames: any[] = []
organizationIds: any[] = []
dis: boolean
selectedCoreOrganization
civilities: string[] = [
new Constants().CIVILITY_MR,
new Constants().CIVILITY_MS,
new Constants().CIVILITY_MRS
]
// Event fired after view is initialized
@ViewChild('organizations') organizations: MatSelect
@ViewChild('roles') roles: MatSelect
constructor(
private fb: FormBuilder,
private dataService: DataService,
private authenticationService: AuthenticationService,
private router: Router,
) { }
ngOnInit() {
this.form = this.fb.group({
civility: [null, Validators.required],
firstName: [null, Validators.required],
lastName: [null, Validators.required],
email: [null, Validators.compose([Validators.required, Validators.email])],
password: [null, Validators.compose([Validators.required, Validators.minLength(8)])],
confirmPassword: [null, Validators.required],
function: [null],
phone: [null],
coreOrganizations: [null, Validators.required],
coreAgencies: [null, Validators.required],
coreRoles: [null, Validators.required]
}, {
validator: PasswordValidation.MatchPassword // your validation method
}
);
let loggedUser = this.authenticationService.getUserFromStorage()
this.spinnerstate = true;
this.dataService.getCollection(new CoreOrganization(), '/get_for_new_user').subscribe(
(response) => {
if (loggedUser.selectedOrganizationId === null)
{
this.coreOrganizations = response.getMember()
this.spinnerstate = false;
}
else {
this.coreOrganizations.push(response)
this.selectedCoreOrganization = response
this.spinnerstate = false;
}
});
}
}
在我的组件上我在订阅时做了些什么。所以我必须在重新生成令牌后重新启动组件。
我怎么做不到。 我的目标是重新使用当前组件。