简体   繁体   English

Angular 9 - 在路由之间共享数据

[英]Angular 9 - Sharing Data Between Routes

I'd like to share data between routes, instead of getting it from the API on each component load.我想在路由之间共享数据,而不是在每个组件加载时从 API 获取数据。

Repo : https://github.com/theADAMJR/2PG-Dashboard回购https://github.com/theADAMJR/2PG-Dashboard

Currently when a user loads a /dashboard/:id/... route, this is called:当前,当用户加载 /dashboard/:id/... 路由时,这被称为:

this.roles = await this.guildService.getRoles(this.guildId);

I'd only like this to be called once when a user loads any dashboard route: /dashboard/:id/... .我只希望在用户加载任何仪表板路由时调用一次/dashboard/:id/...

How would I achieve this?我将如何实现这一目标?

There are multiple ways to accomplish this.有多种方法可以实现这一点。 In its simplest form, you can just append your data to the routes state object:在其最简单的形式中,您可以将 append data发送到路由state object:

this.router.navigate(['/some-route'], {state: {data: {...}}});

https://angular.io/guide/router https://angular.io/guide/router

You could also do:你也可以这样做:

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

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