https://stackblitz.com/github/diefenbach/ng-routing-issue1
从Home移到Item时,Main会再次实例化(从Home移到AnotherItem时不是这样)。
这是预期的行为吗?如果是这样,当您想在Main中做一些独特的事情,例如最初从服务器加载数据时,您将如何处理?除了让所有路由都在app-routing.ts中?
更新:一个好的解决方案似乎是:
导出items-routing.module.ts中的路由:
import { ItemComponent } from './item/item.component';
export const ItemRoutes = [
{ path: 'item', component: ItemComponent },
];
并将其导入app-routing.module.ts中:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AnotherItemComponent } from './another-item/another-item.component';
import { ItemRoutes } from './items/items-routing.module';
import { LoginComponent } from './login/login.component';
import { MainComponent } from './main/main.component';
const routes: Routes = [
{ path: '', component: MainComponent, children: [
{ path: 'another', component: AnotherItemComponent },
[...ItemRoutes,
]},
{ path: 'login', component: LoginComponent },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
预先感谢 凯