[英]Child routing in Angular 2 application
I try to implement child route in my ng2-app. 我尝试在我的ng2-app中实现子路由。 After starting app i get exception "Component "AppComponent" has no route config."
启动应用程序后,我得到异常“组件”AppComponent“没有路由配置。” Here is my code below:
这是我的代码如下:
main.ts: main.ts:
import {bootstrap} from '@angular/platform-browser-dynamic';
import {AppComponent} from './app.component';
import {FORM_PROVIDERS} from "@angular/forms";
import {HTTP_PROVIDERS} from "@angular/http";
import {AuthenticationService} from "./authentication/authentication.service";
import {provideRouter} from "@angular/router";
import {routes} from "./app.routes";
import {ROUTER_PROVIDERS} from "@angular/router-deprecated";
bootstrap(AppComponent, [
provideRouter(routes),
ROUTER_PROVIDERS,
FORM_PROVIDERS,
HTTP_PROVIDERS,
AuthenticationService
]);
app.routes.ts: app.routes.ts:
export const routes:RouterConfig = [
{path: '', component: HomeComponent, canActivate: [AuthenticationService]},
{path: 'login', component: LoginComponent},
{path: 'signup', component: SignupComponent},
{path: 'home', component: HomeComponent, canActivate: [AuthenticationService], children: [
{path: 'first-component', component: FirstComponent},
{path: 'second-component', component: SecondComponent}
]},
{path: '**', component: HomeComponent, canActivate: [AuthenticationService]}
];
app.component.ts: app.component.ts:
import {Component} from '@angular/core';
import {ROUTER_DIRECTIVES, Router} from '@angular/router';
@Component({
selector: 'my-app',
template: `
<router-outlet></router-outlet>
`,
directives: [ROUTER_DIRECTIVES]
})
export class AppComponent {
constructor(public router:Router) {
}
}
home.component.ts: home.component.ts:
import {Component} from "@angular/core";
import {SidebarComponent} from "./sidebar/sidebar.component";
import {HeaderComponent} from "./header/header.component";
import {ROUTER_DIRECTIVES} from "@angular/router-deprecated";
import {Router} from "@angular/router";
@Component({
selector: 'trinjer-home',
template: `
<header></header>
<sidebar></sidebar>
<router-outlet></router-outlet>
`,
directives: [SidebarComponent, HeaderComponent, ROUTER_DIRECTIVES]
})
export class HomeComponent {
constructor(private router: Router) {
}
}
Can't understand, what i do wrong, how to configure this child routing inside home.component? 无法理解,我做错了,如何在home.component中配置这个子路由?
Thanks! 谢谢!
You must set one of the children as a defalut for the parent component by setting the path as an empty string 您必须通过将路径设置为空字符串,将其中一个子项设置为父组件的defalut
{path: 'home', component: HomeComponent, canActivate: [AuthenticationService], children: [
{path: '', redirectTo: 'first-component',
{path: 'first-component', component: FirstComponent},
{path: 'second-component', component: SecondComponent}
]},
check this from the official documentation 从官方文档中查看
https://angular.io/docs/ts/latest/guide/router.html
https://angular.io/docs/ts/latest/guide/router.html
Also check this tutorial it might help you understand it more 另请查看本教程,它可能会帮助您更好地理解它
http://angular-2-training-book.rangle.io/handout/routing/child_routes.html
http://angular-2-training-book.rangle.io/handout/routing/child_routes.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.