[英]How to transfer data between unrelated pages?Angular 8
I am developing an application on Angular 8. I make it multi-page, and I pass the parameters through Router.我正在 Angular 8 上开发一个应用程序。我将它设为多页,并通过路由器传递参数。 I need to transfer data from one page to another, how can I do this if my pages are not connected in any way?
我需要将数据从一个页面传输到另一个页面,如果我的页面没有以任何方式连接,我该怎么做? All pages are loaded via router-outlet.
所有页面都通过路由器插座加载。
Is it bad practice that I don't use parent.component -> child.component nesting?我不使用 parent.component -> child.component 嵌套是不好的做法吗?
Because in the future I will need to transfer data deep into the applications for my library.Example <my-library [data]="data"></my-library>
因为将来我需要将数据深入传输到我的库的应用程序中。例如
<my-library [data]="data"></my-library>
Routes路线
const appRoutes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'progects/:id', component: ProgectsComponent},
{ path: 'progect/:id', component: ProgectComponent},
{ path: 'polls', component: PollsComponent},
{ path: 'poll/:id', component: PollComponent},
{ path: '**', redirectTo: '/' }
];
You have the three ways for transfer data.您可以通过三种方式传输数据。
- Using Router.navigate()
使用 Router.navigate()
this.router.navigate(['/polls'], {
state: {
data: "some data which you want"
}
});
and get it into PollsComponent by this.router.getCurrentNavigation().extras.state
but YOU NEED TO GET THE DATA INTO CONSTRUCTOR of PollsComponent
, on angular lifehooks it is not exists, because it is exists until router event NavigationEnd
.并通过
this.router.getCurrentNavigation().extras.state
将其放入 PollsComponent 但YOU NEED TO GET THE DATA INTO CONSTRUCTOR of PollsComponent
,在角度生命钩子上它不存在,因为它存在直到路由器事件NavigationEnd
。
- Using queryParams
使用查询参数
this.router.navigate(['/polls'], {
queryParams: {
data: "some data which you want"
}
});
and get it into PollsComponent by ActivatedRoute.snapshot.queryParams
并通过
ActivatedRoute.snapshot.queryParams
将其放入 PollsComponent
- Using Singleton service just have a service which is
providedIn: root
and store it by property使用 Singleton 服务只需要提供一个服务
providedIn: root
并按属性存储它
But the data will lost after refresh or when user deletes the queryParams (#2 way).但是数据将在刷新后或用户删除 queryParams 后丢失(#2 方式)。
I will recommend you to use params like poll/:id
and get id into PollComponent onInit method and using that Id get data from.我会建议您使用
poll/:id
并将 id 放入 PollComponent onInit 方法并使用该 Id 从中获取数据。 In this case, you can get :id
in ActivatedRoute.snapshot.params
在这种情况下,您可以在
ActivatedRoute.snapshot.params
获取:id
There are a lot of ways to transfer data between different components.有很多方法可以在不同组件之间传输数据。 The choice of selection may vary based on what your goal is.
选择的选择可能因您的目标而异。
@Input()
decorator@Input()
装饰器@Output()
and EventEmitter
@Output()
和EventEmitter
@ViewChild()
@ViewChild()
You need to create a service for sharing data between different components.您需要创建一个服务来在不同组件之间共享数据。
You can have a look at this article to get started.你可以看看这篇文章开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.