Iam using angular2 router to configure routing for my application Here is my code snippet.
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { AppComponent } from './app.component';
import { DashboardComponent } from './dashboard/dashboard.component';
import { UpdateprofileComponent } from './updateprofile/updateprofile.component';
import { ChangepasswordComponent } from './changepassword/changepassword.component';
import { CookieService } from 'angular2-cookie/services/cookies.service';
import { PathLocationStrategy, LocationStrategy, HashLocationStrategy } from '@angular/common';
const routes = [
{
path: 'dashboard',
component: DashboardComponent
},
{ path: 'updateprofile', component: UpdateprofileComponent },
{ path: 'changepassword', component: ChangepasswordComponent },
// Not found
{ path: '**', redirectTo: 'dashboard' }
];
@NgModule({
declarations: [
AppComponent,
DashboardComponent,
UpdateprofileComponent,
ChangepasswordComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
NgbModule.forRoot(),
RouterModule.forRoot(routes, { useHash: true })
],
providers: [CookieService],
bootstrap: [AppComponent]
})
export class AppModule { }
header.component.html
<header>
<select [(ngModel)]="userOperationType" (ngModelChange)="fnSetOperationType(userOperationType)" class="col-md-12 select-style">
<option value="dashboard">Account Administration</option>
<option value="changepassword" selected>Change Password</option>
<option value="updateprofile">Your Profile</option>
</select>
</header>
header.component.ts
import { Component, OnInit,Input } from '@angular/core';
import { UserDetailsService } from '../services/user-details.service';
import { Router } from '@angular/router';
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.scss'],
providers: [UserDetailsService]
})
export class NvidiaHeaderComponent implements OnInit {
constructor() { }
fnSetOperationType(routeName) {
this.route.navigate([routeName]);
}
}
index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Angular2Routing</title>
<base href="./">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root>loading...
</app-root>
<script type = "text/javascript" >
history.pushState(null, null, '/#/dashboard');
window.addEventListener('popstate', function(event) {
history.pushState(null, null, '/#/dashboard');
});
</script>
</body>
</html>
This is my routing in app.module.ts. After navigating to changepassword route my url is http://localhost:4200/#/changepassword. When iam refreshing my page or opening this url in new tab my url is redirecting to http://localhost:4200/#/dashboard (default route).
I need to get the same route after refreshing my page or if i copy paste the route in another tab i need to get corresponding route.
Please help me. Thanks in advance.
I have done a small mistake in index.html. I have removed the following script. I used it to work on clearing browser history.
<script type = "text/javascript" >
history.pushState(null, null, '/#/dashboard');
window.addEventListener('popstate', function(event) {
history.pushState(null, null, '/#/dashboard');
});
</script>
It worked like charm !!! Thanks for the help.:)