简体   繁体   English

angular-在导航到另一条路线之前将queryParams传递到活动路线

[英]angular - passing queryParams to active route before navigating to another route

I have a clarity datagrid where clicking on a row will open the item by routing from /organization/overview to /organization/:id; 我有一个清晰的数据网格,其中单击一行将通过从/ organization / overview路由到/ organization /:id来打开该项目;

For UX reasons I would like to store the last clicked id as a queryParam before I navigate. 由于UX的原因,我想在导航之前将最后单击的ID存储为queryParam。

eg 例如

  1. user clicks item with id=1234 用户点击ID = 1234的项目
  2. route gehts updated to /organization/overview?id=1234 路线地理位置已更新为/ organization / overview?id = 1234
  3. Navigate to /organization/1234 导航到/ organization / 1234

The idea is that when the user clicks the browser back button, the last clicked item will be highlighted. 这个想法是,当用户单击浏览器后退按钮时,最后单击的项目将突出显示。 I do know that this behaviour could be easily implemented otherwise but this approach is what I'm interested in. Below is the the openOrganization Method I have so far. 我确实知道可以很容易地实现此行为,但是这种方法是我感兴趣的。下面是我到目前为止拥有的openOrganization方法。

openOrganization(organization){
    this.router.navigate([], {
        queryParams: { oid: organization.id },
        relativeTo: this.activatedRoute
    });
    this.router.navigate(['/admin/organizations', organization.id]);
}

It does not work because the 2nd navigate call seems to interrupt or cancel the first on, before its finished. 它不起作用,因为第二个导航调用似乎在完成之前中断或取消了第一个导航。

Thanks! 谢谢!

You should subscribe to your Router and snapshot your current route when the navigation end. 您应该订阅您的路由器,并在导航结束时快照当前路线。
for example: 例如:

lastUrl: string;
constructor(private _router: Router) {
  _router.events.filter(event => event instanceof NavigationEnd).subscribe(e => {
    this.lastUrl = e.url;
  });
}

And then, the lastUrl variable holds the route that you came from. 然后,lastUrl变量保存您来自的路由。
--- Edit -编辑
Same with params, if you have specific params on each url, you can instead of get the url of the last route, get any params that you pass through. 与params相同,如果每个url上都有特定的params,则可以获取传递的所有params,而不是获取最后一条路径的url。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM