Skip to content

Commit

Permalink
Merge pull request #190 from ngx-rocket/bugfix/shell-single-instance
Browse files Browse the repository at this point in the history
Added custom route reuse strategy
  • Loading branch information
sinedied committed Nov 27, 2017
2 parents a109a58 + 44854aa commit 7eb4658
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
7 changes: 6 additions & 1 deletion generators/app/templates/src/app/core/_core.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NgModule, Optional, SkipSelf } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { RouteReuseStrategy, RouterModule } from '@angular/router';
import { HttpModule, Http, XHRBackend, ConnectionBackend, RequestOptions } from '@angular/http';
import { TranslateModule } from '@ngx-translate/core';
<% if (props.ui === 'bootstrap') { -%>
Expand All @@ -18,6 +18,7 @@ import { ShellComponent } from './shell/shell.component';
<% if (props.ui === 'bootstrap' || (props.ui === 'material' && props.layout === 'simple')) { -%>
import { HeaderComponent } from './shell/header/header.component';
<% } -%>
import { RouteReusableStrategy } from './route-reusable-strategy';
<% if (props.auth) { -%>
import { AuthenticationService } from './authentication/authentication.service';
import { AuthenticationGuard } from './authentication/authentication.guard';
Expand Down Expand Up @@ -69,6 +70,10 @@ export function createHttpService(backend: ConnectionBackend,
provide: Http,
deps: [XHRBackend, RequestOptions, HttpCacheService],
useFactory: createHttpService
},
{
provide: RouteReuseStrategy,
useClass: RouteReusableStrategy
}
]
})
Expand Down
10 changes: 5 additions & 5 deletions generators/app/templates/src/app/core/_route.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ export class Route {
return [{
path: '',
component: ShellComponent,
<% if (props.auth) { -%>
children: routes,
canActivate: [AuthenticationGuard]
<% } else { -%>
children: routes
<% if (props.auth) { -%>
canActivate: [AuthenticationGuard],
<% } -%>
}];
// Reuse ShellComponent instance when navigating between child views
data: { reuse: true }
}];
}

}
28 changes: 28 additions & 0 deletions generators/app/templates/src/app/core/route-reusable-strategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ActivatedRouteSnapshot, DetachedRouteHandle, RouteReuseStrategy } from '@angular/router';

/**
* A route strategy allowing for explicit route reuse.
* Used as a workaround for https://github.com/angular/angular/issues/18374
* To reuse a given route, add `data: { reuse: true }` to the route definition.
*/
export class RouteReusableStrategy extends RouteReuseStrategy {

public shouldDetach(route: ActivatedRouteSnapshot): boolean {
return false;
}

public store(route: ActivatedRouteSnapshot, detachedTree: DetachedRouteHandle): void { }

public shouldAttach(route: ActivatedRouteSnapshot): boolean {
return false;
}

public retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
return null;
}

public shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
return (future.routeConfig === curr.routeConfig) || future.data.reuse;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
<nav>
<mat-list>
<mat-divider></mat-divider>
<a mat-list-item routerLink="/home" routerLinkActive="active">
<a mat-list-item routerLink="/home" routerLinkActive="active" (click)="sidenav.close()">
<span translate>Home</span>
</a>
<mat-divider></mat-divider>
<a mat-list-item routerLink="/about" routerLinkActive="active">
<a mat-list-item routerLink="/about" routerLinkActive="active" (click)="sidenav.close()">
<span translate>About</span>
</a>
<mat-divider></mat-divider>
Expand Down
2 changes: 1 addition & 1 deletion generators/app/templates/src/app/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class HomeComponent implements OnInit {
quote: string;
isLoading: boolean;

constructor(private quoteService: QuoteService) {}
constructor(private quoteService: QuoteService) { }

ngOnInit() {
this.isLoading = true;
Expand Down

0 comments on commit 7eb4658

Please sign in to comment.