user3334871
user3334871

Reputation: 1361

Angular 2 routing removes URL path

I added routing to my angular 2 app to read query parameters from the URL. The URL looks something like www.myapp.com/projectName/myPage.html?query1=firstName. In my routing code, I have:

import { ModuleWithProviders }  from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { routingComponent } from './app.adminPage';

// Route Configuration
export const routes: Routes = [
  { path: 'myPage.html', component: routingComponent}
];


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

My example is very simple, as all I am using routing for is reading query parameters from the page. However, when I hit www.myapp.com/projectName/myPage.html?query1=firstName and it routes and serves my component, the URL changes to www.myapp.com/projectName/, which means that if the user refreshes, it loads back to my landing page. I have another page that uses routing to ready query params that doesn't do this, so I am curious as to why this page does that. Is there a way to stop this?

Upvotes: 2

Views: 1077

Answers (1)

Arun Kumaresh
Arun Kumaresh

Reputation: 6311

export const routes: Routes = [ { path: 'myPage/:query1', component: routingComponent} ]; 

set parameters in the path now call the component using the URL www.myapp.com/projectName/myPage/firstname

Upvotes: 1

Related Questions