首页 > 解决方案 > 角度,在重定向中获取标题?

问题描述

我有另一个地址将添加到我在 Angular 9/10 中开发的地址中。它将在标题中传递一些参数,我想在访问我的“/ home”时接收它并从那里采取一些行动。

流程:另一个站点(添加标题)并重定向>我的站点...

ngOnInit() 开始检查是否收到任何标头?

标签: angulartypescript

解决方案


Normally ActivatedRoute can be used to get the header data. If you check the documentation of ActivatedRoute, you can use "data" instead of params and get the values.

For an example here based on documentation: https://angular.io/api/router/ActivatedRoute#description

import {Component} from '@angular/core';
/* . . . */
import {ActivatedRoute} from '@angular/router';
import {Observable} from 'rxjs';
import {map} from 'rxjs/operators';
/* . . . */

@Component({
/* . . . */
})
export class ActivatedRouteComponent {
  constructor(route: ActivatedRoute) {
    const id: Observable<string> = route.params.pipe(map(p => p.id));
    const url: Observable<string> = route.url.pipe(map(segments => segments.join('')));
    // route.data includes both `data` and `resolve`
    const user = route.data.pipe(map(d => d.user));
  }
}

Here if you see the const user = route.data.pipe(map(d => d.user));, this is what you may need to do.


推荐阅读