I'm currently working on implementing a basic route with a parameter in Angular2. My setup involves Angular 2.0.0-rc.2 and angular router 3.0.0-alpha.7. I've mostly relied on the updated routing documentation available at https://angular.io/docs/ts/latest/guide/router.html
index.html
<!doctype html>
<html>
<head>
<base href=".">
...
main.ts
import { bootstrap } from '@angular/platform-browser-dynamic';
import { AppComponent } from './app';
import { HTTP_PROVIDERS } from '@angular/http';
import { APP_ROUTER_PROVIDERS } from './app/app.routes';
bootstrap(AppComponent, [
HTTP_PROVIDERS,
APP_ROUTER_PROVIDERS
])
.catch(err => console.error(err))
;
app.routes.ts
export const routes: RouterConfig = [
{ path: 'featured', component: FeaturedPageComponent },
{ path: 'containers/:id', component: ContainerEditComponent }
];
export const APP_ROUTER_PROVIDERS = [
provideRouter(routes)
];
container-edit.component.ts
@Component({
moduleId: module.id,
selector: 'container-edit',
templateUrl: 'container-edit.component.html',
styleUrls: ['container-edit.component.css']
})
export class ContainerEditComponent implements OnInit, OnDestroy {
private sub: any;
constructor(
private route: ActivatedRoute,
private router: Router
) {
}
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
let id = +params['id']; // + converts string to number
console.log(id);
});
}
ngOnDestroy() {
this.sub.unsubscribe();
}
}
When trying to access
http://localhost:4200/containers/1337
, I come across the following error message.
EXCEPTION: Error: Uncaught (in promise): Error: Cannot match any routes: '1337'