[英]Child routing using angular 2
I am very much new to angular 2. I was trying to build a simple application with Login and Dashboard Pages. 我对angular 2非常陌生。我试图使用Login和Dashboard Pages构建一个简单的应用程序。 I need to understand how to apply the routing as my application needs.
我需要了解如何应用路由作为我的应用程序需要。
Login page is an independent route config, but i want when use signed in, new new dashboard page comes with navigation bar and its own <router-outlet>
. 登录页面是一个独立的路由配置,但我想在使用登录时,新的仪表板页面附带导航栏和自己的
<router-outlet>
。
const routes: Routes = [
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: 'register-user', component: RegisterUserComponent },
//APPLICATION ROUTES WITH OWN <router-outlet>
{ path: '**', redirectTo: 'login' }
]
Earlier in angular 1, we have used ui-router with abstract state and child states. 在角度1的早期,我们使用了具有抽象状态和子状态的ui-router。
Please suggest how we can accomplish that. 请建议我们如何实现这一目标。
Working plunkr 工作的plunkr
https://plnkr.co/edit/MqNv6RyQvzsiZTp0Dkpf?p=preview https://plnkr.co/edit/MqNv6RyQvzsiZTp0Dkpf?p=preview
Create one ContainerComponent & it should hold its own router-outlet & routerLinks. 创建一个ContainerComponent,它应该拥有自己的router-outlet和routerLinks。
By default, always load to anyComponent using redirectTo 默认情况下,始终使用redirectTo加载到anyComponent
{ path: 'component-two', component: ComponentTwo,
children: [
{ path: '', redirectTo: 'child-one', pathMatch: 'full' },
{ path: 'child-one', component: ChildOne },
{ path: 'child-two', component: ChildTwo }
]
}
Suppose u have a submodule inside main module. 假设你在主模块中有一个子模块。
So App module and login sub module 所以App模块和登录子模块
directory structure of main Module 主模块的目录结构
app.routing.module.ts something looks like this app.routing.module.ts看起来像这样
export const routes: Routes = [
{ path: '', component:LoginComponent, pathMatch: 'full'},
{ path: Constants.LOGINROUTE, component:LoginComponent, pathMatch: 'full'}
];
@NgModule({
imports: [RouterModule.forRoot(routes,{ useHash: true })],
exports: [RouterModule]
})
and corresponding login.routing.module.ts 和相应的login.routing.module.ts
@NgModule({
imports: [RouterModule.forChild([
{ path: Constants.LOGINROUTE, component: LoginComponent}
])],
exports: [RouterModule]
})
export class LoginRoutingModule {}
and dashboard.routing.module.ts will look like this 和dashboard.routing.module.ts看起来像这样
@NgModule({
imports: [RouterModule.forChild([
{ path: Constants.DASHBOARDROUTE, component: DashboardComponent}
])],
exports: [RouterModule]
})
export class DashboardRoutingModule {}
correspoding include login.routing.module.ts in the login.module.ts and same for dashboard.routing.module.ts correspoding包括login.module.ts中的login.routing.module.ts和dashboard.routing.module.ts中的相同内容。
I have done this in my project. 我在我的项目中完成了这个。
This is used for lazy-loading for child route: 这用于子路径的延迟加载:
You should use loadChildren
like this in your main app-routing.module.ts
: 你应该在你的主
app-routing.module.ts
使用这样的loadChildren
:
{ path: 'crisis-center',
loadChildren: 'app/modules/crisis-center/crisis.module#CrisisCenterModule'
},
And then remove component
from your app-module.ts file
in below code : 然后在以下代码中从
app-module.ts file
中删除component
:
app.module.ts : app.module.ts:
@NgModule({
imports: [ RouterModule.forRoot(routes, { useHash: true })
// ,AdminModule
],
declarations: [ ],
providers: [ ],
bootstrap: [ AppComponent ]
})
app-routing.module.ts: APP-routing.module.ts:
export const routes: Routes = [
{ path: 'dashboard', component: DashboardComponent },
{ path: 'admin',
loadChildren: 'app/admin/admin.module#AdminModule'
},
{ path: 'crisis-center',
loadChildren: 'app/modules/crisis-center/crisis.module#CrisisCenterModule'
},
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: '**', component: PageNotFound},
];
Now fine.When the router
navigates to this route, it uses the loadChildren
string to dynamically load the AdminModule
. 现在很好。当
router
导航到此路由时,它使用loadChildren
字符串动态加载AdminModule
。 Then it adds the AdminModule
routes to its current route configuration. 然后,它将
AdminModule
路由添加到其当前路由配置。 Finally, it loads the requested route to the destination admin component. 最后,它将请求的路由加载到目标管理组件。
You can also use this ( children[ ] ) in your routing
file and can give separate component for each child routes: 您也可以在
routing
文件中使用this(children []),并为每个子路径提供单独的组件:
const crisisCenterRoutes: Routes = [
{
path: '',
component: CrisisCenterComponent,
children: [
{
path: '',
component: CrisisListComponent,
children: [
{
path: ':id',
component: CrisisDetailComponent,
canDeactivate: [CanDeactivateGuard]
},
{
path: '',
component: CrisisCenterHomeComponent
}
]
}
]
}
];
And then add all the components into your particular module.ts
file , like this: 然后将所有组件添加到您的特定
module.ts
文件中,如下所示:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { CrisisCenterRoutingModule } from './crisis-center-routing.module';
import { CrisisCenterComponent } from './crisis-center.component';
import { CrisisListComponent } from './crisis-list.component';
import { CrisisDetailComponent } from './crisis-detail.component';
import { CrisisCenterHomeComponent } from './crisis-center-home.component';
import { CrisisService } from './crisis.service';
@NgModule({
imports: [ CommonModule,CrisisCenterRoutingModule,FormsModule, ],
declarations: [CrisisCenterComponent ,CrisisListComponent,CrisisDetailComponent,CrisisCenterHomeComponent ],
providers: [ CrisisService ],
})
export class CrisisCenterModule {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.