Passing data through Angular2 router

后端 未结 7 790
暖寄归人
暖寄归人 2020-12-24 14:01

I have a component where I select a set of image objects. I want to pass these selected images to my new route, CreateAlbum. The data it nested and wouldn\'t be suitable to

7条回答
  •  被撕碎了的回忆
    2020-12-24 14:15

    You can pass data using router, for example

    { path: 'form', component: FormComponent ,data :{data :'Test Data'} },
    

    and your component

    import { ActivatedRoute, Router } from '@angular/router';
    
    export class FormComponent {
        sub: any;
        constructor(private route: ActivatedRoute) { }
        ngOnInit() {
            this.sub = this.route
                .data
                .subscribe(value => console.log(value));
        }
    }
    

    More info

    But this may not be the preferred way, you can use parent child communication or make a service common and share data between them. Check below references for doing that.

    Parent child communication

    Share data using service

提交回复
热议问题