首页 > 解决方案 > 使用角度将隐藏参数传递给外部链接

问题描述

我想从角度路由器导航并将一些隐藏参数传递到外部链接。

假设我有一个在 www.demo.com 上运行的 Angular 应用程序,我想使用 Angular 路由器导航到带有一些隐藏参数的 www.xyz.com。

我尝试使用 router.navigate() 但我猜它只适用于应用程序,不适用于外部链接。

html

<a class="UserActions-editButton edit-button EdgeButton EdgeButton--tertiary" data-scribe-element="profile_edit_button" type="button" (click)="enterUrl($event)">
<span class="button-text">Enter Url</span>
</a>

零件

enterUrl(event:any){
    var final_url = 'www.xyz.com'
    this.router.navigate([final_url], { queryParams: {token: this.token}});
  }

请提出解决方案。谢谢

标签: angularangular5angular-router

解决方案


不,您不能将隐藏参数发送到任何外部链接。

但下面是 Angular 应用程序内部导航的用例。

IMO 为您的用例data属性将帮助您实现您的要求。

const routes: RouterConfig = [
  {path : 'heroes', component : HeroDetailComponent, data : {some_data : 'some value'}}
];

所以,你会像这样得到你通过的内容 -

this.route.data.subscribe(data => console.log(data));

PS:但据我所知,您只能使用data属性发送静态数据。

第三个路由中的data属性是存储与此特定路由关联的任意数据的地方。在每个激活的路由中都可以访问 data 属性。使用它来存储页面标题、面包屑导航文本和其他只读静态数据等项目。您将在本指南后面使用解析保护来检索动态数据。


推荐阅读