Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass invisible or hidden parameters using routerLink Angular

I have router link like below:

<button class="take-a-tour-btn" [routerLink]="['/dashboard', {'showTour':'show'}]">

I want to pass parameter showTour. But, when I do this, the parameter is visible in url and I would like to hide it. I have gone through so many references(which says about optional parameters) ,but with no success in my case. How could I solve this?

like image 868
Sai Avatar asked Oct 24 '17 08:10

Sai


People also ask

What is difference between RouterLink and RouterLink?

What is the difference between [routerLink] and routerLink ? How should you use each one? They're the same directive. You use the first one to pass a dynamic value, and the second one to pass a static path as a string.

What is difference between RouterLink and href?

Href is the basic attribute provided by Html to navigate through pages which reloads the page on click. routerLink is the attribute provided by angular to navigate to different components without reloading the page.

What does RouterLink do in angular?

RouterLink is a built-in Angular Directive that lets you link to specific routes in your app. In the SPA(single-page application), you change what the user sees by showing or hiding portions of the display that correspond to particular components, rather than going out to the server to get a new page.

How do I use RouterLink in anchor tag?

Adding routerLink attribute in anchor tag. Now, To make any element as link we must enclose it in anchor tag, therefore we will use <a> tag enclosing the <li> tag to make it a link.


3 Answers

You can use History state to pass dynamic data to the component you want to navigate to, without adding them into the URL, like so :

this.router.navigateByUrl('/user', { state: { orderId: 1234 } }); 

or

<a [routerLink]="/user" [state]="{ orderId: 1234 }">Go to user's detail</a> 

and you can get it this way

constructor() {   this.router.events    .pipe(filter(e => e instanceof NavigationStart))    .subscribe((e: NavigationStart) => {     const navigation  = this.router.getCurrentNavigation();     this.orderId = navigation.extras.state ? navigation.extras.state.orderId : 0;    });   } 
like image 102
Fateh Mohamed Avatar answered Sep 21 '22 21:09

Fateh Mohamed


I'm not sure, if there is a way to do it, because the data need to be presented in the URL string.

My suggestion is using global service which be store needed data. For example:

//some dataService, which store your needed data. @Injectable() export class DataService {     _showTour: string;     set showTour(value: string) {       this._showTour = value;    }     get showTour(): string {        return this._showTour;    }     constructor() {}  } 

and use it in your navigation component in this way:

//your navigation component @Component({     selector: 'my-app',     template: `        <button class="take-a-tour-btn" (click)="onClick()">     ` }) export class SomeComponent {     constructor(private dataService: DataService, private router: Router) { }      onClick() {         this.dataService.showTour = 'show';         this.router.navigate(['/dashboard']);     } } 

You will may use the same service in your Dashboard Component, and get needed value, f.e. in this way:

//your dashboard component @Component({     selector: 'my-dashboard',     template: `        <h1>{{ showTour }}</h1>     ` }) export class DashboardComponent implements OnInit {      showTour: string;      constructor(private dataService: DataService) { }      ngOnInit() {         this.showTour = this.dataService.showTour;     } } 
like image 32
Jaroslaw K. Avatar answered Sep 23 '22 21:09

Jaroslaw K.


Use state to pass hidden parameters and history to read 'em.

First component:

this.router.navigate(
      [`/dashboard/roles/${id}`],
      { state: { navSettings: navSettings } });

Second component:

public ngOnInit(): void {
    const id = this.activatedRoute.snapshot.params.id;
    this.initNavSettings(history.state.navSettings);
}
like image 44
Butsaty Avatar answered Sep 22 '22 21:09

Butsaty