Skip to content

Commit 135c38f

Browse files
rafaelss95jelbourn
authored andcommitted
Add handling for invalid routes (angular#191)
1 parent ad1400e commit 135c38f

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/app/pages/component-viewer/component-viewer.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Component, NgModule, ViewEncapsulation} from '@angular/core';
2-
import {ActivatedRoute, RouterModule} from '@angular/router';
2+
import {ActivatedRoute, Router, RouterModule} from '@angular/router';
33
import {DocumentationItems, DocItem} from '../../shared/documentation-items/documentation-items';
44
import {ComponentPageTitle} from '../page-title/page-title';
55
import {MdTabsModule} from '@angular/material';
@@ -17,11 +17,17 @@ export class ComponentViewer {
1717
componentDocItem: DocItem;
1818

1919
constructor(private _route: ActivatedRoute,
20+
private router: Router,
2021
public _componentPageTitle: ComponentPageTitle,
2122
public docItems: DocumentationItems) {
22-
_route.params.subscribe(p => {
23-
this.componentDocItem = docItems.getItemById(p['id']);
24-
this._componentPageTitle.title = `${this.componentDocItem.name}`;
23+
this._route.params.subscribe(params => {
24+
this.componentDocItem = docItems.getItemById(params['id']);
25+
26+
if (this.componentDocItem) {
27+
this._componentPageTitle.title = `${this.componentDocItem.name}`;
28+
} else {
29+
this.router.navigate(['/components']);
30+
}
2531
});
2632
}
2733
}

src/app/routes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ export const MATERIAL_DOCS_ROUTES: Routes = [
3737
{path: 'overview', component: ComponentOverview, pathMatch: 'full'},
3838
{path: 'api', component: ComponentApi, pathMatch: 'full'},
3939
{path: 'examples', component: ComponentExamples, pathMatch: 'full'},
40+
{path: '**', redirectTo: 'overview'},
4041
],
4142
},
4243
],
4344
},
4445
{path: 'guides', component: GuideList},
4546
{path: 'guide/:id', component: GuideViewer},
47+
{path: '**', redirectTo: ''},
4648
];

0 commit comments

Comments
 (0)