您正在添加<router-outlet></router-outlet>
HomeComponent?当您有子路线时,您必须添加<router-outlet></router-outlet>
. 你的问题可能是这样的。
我在这里测试并以这种方式工作。我创建了 4 个组件(HomeComponent、Home1Component、Home2Component 和 Home3Component)。
在 app.routing 中:
import {HomeComponent} from './admin/home/home.component'
import {Home1Component} from './admin/home1/home1.component'
import {Home2Component} from './admin/home2/home2.component'
import {Home3Component} from './admin/home3/home3.component'
export const routing: ModuleWithProviders = RouterModule.forRoot([
{ path: '', component: HomeComponent },
{ path: 'admin', component: Home3Component, data: { title: "Home", icon: "home" } },
{
path: 'test', component: Home1Component, data: { title: "Settings", icon: "settings" },
children: [
{ path: '', component: Home2Component, data: { title: "Test", icon: "power_settings_new" } },
{ path: 'settings', component: Home3Component, data: { title: "Test2", icon: "power_settings_new" } }
]
}
]);
在 Home1Component 你必须把(孩子将被加载到):
<router-outlet></router-outlet>
在 app.module 中:
import { AppComponent } from './app.component';
import { HomeComponent } from './admin/home/home.component';
import { Home1Component } from './admin/home1/home1.component';
import { Home2Component } from './admin/home2/home2.component';
import { Home3Component } from './admin/home3/home3.component';
@NgModule({
...
declarations: [ AppComponent, HomeComponent, Home1Component, Home2Component, Home3Component ],
...
})
创建路线:
<li><a routerLink="/admin">Admin</a></li>
<li><a routerLink="/test">test</a></li>
<li><a routerLink="/test/settings">teste/settings</a></li>
以这种方式在这里测试并有效!