gog
gog

Reputation: 12988

How to pass an object from one component to another via navigation route?

I cannot find how to pass an complex object from one component to another in Angular.

I have two components in different modules in my app, i would like to navigate from one component to another an pass a complex object to the second component.

Component One:

buttonSubmit() {
    this._router.navigate(['/app/sales/history',  { product: this.product}]);
}

So in the component two i would like to take the product object:

 this._route.params.subscribe(v => console.log(v));

I tried using ActivatedRoute but it doesnt work, on the Angular docs they have examples of parent/child communication, but its not the case.

Upvotes: 1

Views: 920

Answers (1)

Dmitry Shutov
Dmitry Shutov

Reputation: 96

Make service and save data into service. On Component One:

ngOnDestroy() {
  this.ProductService.product = this.product;
}

On component Two

ngOnOnit() {
  this.product = this.ProductService.product;
}

In component One parent module some.module.ts add ProductService to providers:

@NgModule({
providers: [ProductService]
})

in component Two parent module other.module.ts add ProductService to imports:

 @NgModule({
    imports: [ProductService]
    })

Upvotes: 1

Related Questions