【发布时间】:2017-08-10 00:49:49
【问题描述】:
我是 Angular 2 的新手。我想为我的应用程序的每个部分创建独立的模块。例如,我使用默认组件创建了AuthModule - AuthComponent,其中包含一个router-outlet 用于他的子组件(SignIn 或 SignUp)。所以我想实现以下场景:
- 导航到 / 时 - 根除应用程序 - 重定向到 /auth
- 重定向到 /auth 后 - 使用路由器出口加载 AuthComponent
- AppComponent 加载后 - 通过重定向到 /auth/sign-in 加载默认登录组件
但是当我去 localhost/ 时,我得到了我想要的重定向到 /auth,但是下一个重定向到登录没有出现。
我的代码:
app.routing
const routes: Routes = [
{
path: '',
redirectTo: '/auth',
pathMatch: 'full'
}
];
export const appRouting: ModuleWithProviders = RouterModule.forRoot(routes);
auth.routing
const routes: Routes = [
{
path: 'auth',
component: AuthComponent,
children: [
{
path: '',
redirectTo: 'sign-in',
pathMatch: 'full'
},
{
path: 'sign-in',
component: SignInComponent
}
]
},
];
export const authRouting: ModuleWithProviders = RouterModule.forChild(routes);
auth.component.html
<div class="container">
<h1>Auth component</h1>
<router-outlet></router-outlet>
</div>
结果:
环境@angular/cli:1.0.0-rc.2 节点:7.7.1 操作系统:win32 x64
【问题讨论】:
-
将子路由直接放在主路由中,不要单独定义
-
我将所有路由都放到了应用路由中,但它仍然没有按预期工作。