ore ore
ore ore

Reputation: 89

Angular 7 router.navigate with message after redirecting

I'm using this code to redirect this.router.navigate(['/abc']);. How to show some message on the new page using <ngb-alert>, Is it possible to pass some variable while redirecting but not as url query parameters?

Upvotes: 1

Views: 4847

Answers (2)

Abolfazl Roshanzamir
Abolfazl Roshanzamir

Reputation: 14802

You can do that with State as below:

When you want to navigate set state:

const navigationExtras: NavigationExtras = {state: {data: 'This is an example'}};
this.router.navigate(['/abc'], navigationExtras);

In destination component you can get data like the following:

data:string;
constructor(private router: Router) { 
   const navigation = this.router.getCurrentNavigation();
   const state = navigation.extras.state as {data: string};
   this.data = state.data;
}

Upvotes: 6

HusterFrancis
HusterFrancis

Reputation: 21

You can use a service to pass data from one component to another without using route parameters at all.

Their is already a datailed topic about this. Send data through routing paths in Angular

Upvotes: 0

Related Questions