简体   繁体   English

如何在不相关的页面之间传输数据? Angular 8

[英]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.您可以通过三种方式传输数据。

  1. 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

  1. 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

  1. 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 方式)。

Note:笔记:

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.选择的选择可能因您的目标而异。

  1. Parent to child: @Input() decorator父对子: @Input()装饰器
  2. Child to parent: @Output() and EventEmitter子到父: @Output()EventEmitter
  3. Child to parent: @ViewChild()子对父: @ViewChild()
  4. Unrelated component: Sharing Data as a service .无关组件:将数据作为服务共享。

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.

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