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?
Asked
Active
Viewed 4,358 times
1

ore ore
- 79
- 1
- 2
- 6
-
Why not use some service? when you redirect check on ngOnInit for message on side service. easy. – Talg123 Oct 25 '19 at 10:29
-
which Angular version are you using ? Angular 7.2 has state in NavigationExtras. You can use 'state' in NavigationExtras and read that state. – Buzzinga Oct 25 '19 at 10:41
-
@Buzzinga I'm using 7.3. I will test it – ore ore Oct 25 '19 at 11:10
2 Answers
6
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;
}

Abolfazl Roshanzamir
- 12,730
- 5
- 63
- 79
0
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

HusterFrancis
- 21
- 4