6

In Angular 2, is there a way to navigate and pass data that is not shown in the URL?

I want to create a component that receives a complex object, but do not want this to be displayed in the URL.

Thanks!

6
  • Set the data on a service that is injected into the component? Commented Sep 28, 2016 at 0:50
  • 1
    There is a simpler way? I don´t want to make a service class just for that. I have to do a class every time you want to hide the parameters?? Commented Sep 28, 2016 at 1:03
  • No. The router passes dynamic data via the URL. You could serialize and encode your object in the URL, but you'd need to be careful of any characters not allowed in URL strings. A shared service is the recommended way, and cleanest. Commented Sep 28, 2016 at 1:51
  • You can also either save it in cookies, local storage or service. If you think one of them is an option. Commented Sep 28, 2016 at 2:08
  • angular.io/docs/ts/latest/cookbook/component-communication.html Commented Sep 28, 2016 at 5:10

4 Answers 4

6

Only to support new readers who are using new versions of angular, in angular 7.2 and above you can use state to pass data objects dynamically without showing in url.

this.router.navigate(['/some-route'], {state: {data: {...}}});

then you can read state like:

ngOnInit() {
    this.state = this.activatedRoute.paramMap
        .pipe(map(() => window.history.state));}
Sign up to request clarification or add additional context in comments.

1 Comment

skiplocations worked for me today.
3

If you are using navigate method, then add skipLocationChange like this:

this.router.navigate(['/your-path'], { skipLocationChange: true });

If you're using routerLink, use it as an attribute like this:

<a [routerLink]="['/your-path']" skipLocationChange></a>

Please bear in mind that if you use skipLocationChange, the route in the browser won't be updated after navigating to the new route, so if the user hits back in the browser he'll be sent to the path before the first path. I haven't found a solution for this yet.

Comments

3

You can use below function to pass parameters:

this.router.navigate(['/somePage'], {queryParams: {'name': 'Emon'}, skipLocationChange: true});

This works for me!

2 Comments

How to access that name value in Component?
Hi @dev, You can use ActivatedRoute. There is a function called queryParams. For Example: this.activeRoute.queryParams.subscribe(params =>{ this.userName= params['name'];});
0

Yes it can be done.. Below is an example from this link.

const appRoutes: Routes = [
  { path: 'hero/:id', component: HeroDetailComponent },
  { path: 'crisis-center', component: CrisisCenterComponent },
  {
    path: 'heroes',
    component: HeroListComponent,
    data: {
      title: 'Heroes List'
    }
  },
  { path: '', component: HomeComponent },
  { path: '**', component: PageNotFoundComponent }
];

In this example we are passing an object to 'heroes' path which will not be displayed in url.Hope this helps.

2 Comments

Thank´s for the answer, but I need to define de data when the user make some action instead when I define de Routes.
I am not sure , but we can always set appRoutes.path.data.. dynamically

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.