简体   繁体   English

如何通过路由器将数据从角度组件传递到另一个组件?

[英]How do I pass data from angular component to another component via router?

I'm tried to pass data from angular component to another component via a router. 我试图通过路由器将数据从角度组件传递到另一个组件。 Any elegant solution? 任何优雅的解决方

I tried this and it's working. 我试过这个并且它正在工作。

this.router.config.forEach(route => {
    if (route.path === 'pages') {
        route.children.forEach((child) => {
            if (child.path === 'saveName') {
                child.data = {
                    name: "Jack"
                };
            }
        });
    }
});

this.router.navigate(['/pages/saveName']);

And also send a file with data. 并且还发送包含数据的文件。

Just want to know whether there is a more elegant solution, like the one below. 只想知道是否有更优雅的解决方案,如下图所示。

this.router.navigate(['/pages/saveName'],{"name":"Jack"}); this.router.navigate([ '/页/ SAVENAME'],{ “名称”: “杰克”});

是的,请查看文档

this.router.navigate(['/heroes', { id: heroId, foo: 'foo' }]);

I think is better solution.Check following above Link. 我认为是更好的解决方案。请查看上面的链接。

https://angular.io/guide/router#fetch-data-before-navigating https://angular.io/guide/router#fetch-data-before-navigating

Angular 7.2 supports passing data via a state object during navigation Angular 7.2支持在导航期间通过状态对象传递数据

You can pass the state object in navigateByUrl method 您可以在navigateByUrl方法中传递状态对象

this.router.navigateByUrl('/path', { state: { hello: 'world' } });

or 要么

Pass Data using routerLink directive 使用routerLink指令传递数据

<a routerLink="/path" [state]="{ hello: 'world' }">Go</a>`

And Read the data using paramMap Observabele to filter only window.history.state 并使用paramMap Observabele读取数据,仅过滤window.history.state

 constructor(public activatedRoute: ActivatedRoute) {}

  ngOnInit() {
    this.state$ = this.activatedRoute.paramMap.pipe(
      map(() => window.history.state)
    );
  }

Check this link: https://netbasal.com/set-state-object-when-navigating-in-angular-7-2-b87c5b977bb 点击此链接: https//netbasal.com/set-state-object-when-navigating-in-angular-7-2-b87c5b977bb

暂无
暂无

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

相关问题 如何正确地以角度2将数据从组件传递到另一个组件 - How do I properly pass data from a component to another in angular 2 如何在 Angular 中将数据从一个组件传递到另一个组件? - How do I pass data from one component to another in Angular? 在 Angular 8 中,如何将一个组件传递到另一个组件的输入中,然后在路由器模块中强制使用它 - In Angular 8, how do I pass a component into another component's input and then use it imperatively in a router module 如何在 Angular 中将数据从一个组件发送到另一个组件? - How do I send data from a Component to another Component in Angular? Angular - 如何将数据从一个组件传递到另一个 - Angular - how to pass data from a component to another 如何以角度将数据从一个组件传递到另一个组件(新浏览器选项卡)? - How do I pass data from one component to another (New Browser Tab) in angular? 如何将角度2中的数据从一个组件传递到另一个组件? - how to pass data from one component to another component in angular 2? 如何在单击按钮时将 id 和对象从一个组件传递到另一个组件的 angular 函数? - How do I pass an id and object on click of a button from one component to another component' s function in angular? 将数据从路由器出口中的组件传递到“父”组件[Angular 2] - Pass data from component in router-outlet to a "Parent'' component [Angular 2] 我正在做一个Angular项目。 我需要使用router.navigate将数组从一个组件传递到另一个组件,但是无法做到这一点 - I am working on an Angular project. I need to pass an array from one component to another using router.navigate but unable to do it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM