8

I have urls with queryParams and params in my angular 2 app. And i'm stuck with problem: after reloading pages my urls distorted.

with params:

http://127.0.0.1:8000/albums?user_id=1

after reload:

http://127.0.0.1:8000/albums/?user_id=1

with queryParams:

http://127.0.0.1:8000/albums/%3Aid;id=13

after reload:

http://127.0.0.1:8000/albums/%3Aid%3Bid%3D13

routes.ts

import { ModuleWithProviders }  from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { HomeComponent } from './components/home/home.component';
import { LoginComponent }  from './components/login/login.component';
import { RegisterComponent }  from './components/register/register.component';
import { AlbumsComponent }  from './components/albums/albums.component';
import { AddAlbumComponent }  from './components/albums/add-album.component';
import { AddImageAlbumComponent }  from './components/albums/add-image-album.component';
import { AlbumDetailComponent }  from './components/albums/album-detail.component';
import { PhotosComponent }  from './components/photos/photos.component';
import { UsersComponent }  from './components/users/users.component';
import { AuthGuard } from './guards/auth.guard'

const appRoutes: Routes = [
    { path: '', component: HomeComponent },
    { path: 'login', component: LoginComponent },
    { path: 'register', component: RegisterComponent },
    { path: 'albums', component: AlbumsComponent, canActivate: [AuthGuard] },
    { path: 'add-album', component: AddAlbumComponent, canActivate: [AuthGuard] },
    { path: 'add-image-album/:id', component: AddImageAlbumComponent, canActivate: [AuthGuard] },
    { path: 'albums/:id', component: AlbumDetailComponent, canActivate: [AuthGuard] },
    { path: 'photos', component : PhotosComponent, canActivate: [AuthGuard] },
    { path: 'users', component : UsersComponent, canActivate: [AuthGuard] }
];

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

navigation with params:

[routerLink]="['/albums/:id', {id: album.id}]

navigation with queryParams:

[queryParams]="{user_id: dataService.getCurrentUserId()}"
2
  • Looks like something wrong with your navigation code. Can you show your component where you handle navigation. Commented Oct 29, 2016 at 9:37
  • I have the same problem. Commented Feb 24, 2017 at 12:35

2 Answers 2

0

After url decoding your reloaded queryparams url, I get this:

http://127.0.0.1:8000/albums/:id;id=13

It is essentially the same as your original url, but with the params fully url encoded.

Update:

If you need a reference on how to force decode the url, you can look at this previous stack overflow question

Sign up to request clarification or add additional context in comments.

Comments

0

Pass the param in RouteLink itself for exmaple see the below code

[routerLink]="[/albums,nav.param]"

And in Routing declare like this

{ path: 'albums/:id', component: AlbumsComponent }

And in albumscomponent.ts read the url param value using activateroute.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.