Angular 中模块之间的路由
Routing between Modules in Angular
我正在构建简单的 angular 应用程序。有两个模块 student 和 teacher。
这是我的项目组织方式。
首先当用户进入应用程序时,我让他选择他是老师还是学生。
根据他的用户将被重定向到相应的模块。
import {NgModule} from '@angular/core';
import {Routes, RouterModule} from '@angular/router';
import { StudentModule } from './student/student.module';
import { TeacherModule } from './teacher/teacher.module';
import { HomeComponent } from './home/home.component';
const routes: Routes = [
{
path: '',
component: HomeComponent
},
{
path: 'student',
loadChildren: () => StudentModule
},
{
path: 'teacher',
loadChildren: () => TeacherModule
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {
}
这是我的 app.routing.ts
文件。
我的问题是当我重定向到我想在这些模块中的组件之间路由的模块时。我应该在每个模块中添加另一个 <router-outlet>
还是我可以用 AppModule
中唯一的 <router-outlet>
来做到这一点?
如果我应该添加另一个 <router-outlet>
我应该如何为这些模块编写我的路由器 class。
是的,您需要为各个模块定义路由,并在您需要提供的模块组件文件中提供
下面应该是文件结构
- Teacher
-teacher.component.html --> here you should put <router-outlet>
-techer-routing.module.ts --> here you need to define routes for teacher module
-teacher.module.ts --> import techer-routing.module here
-Logincomponent
-login.component.html
-login.component.ts
-Homecomponent
-home.component.html
-home.component.ts
与学生的另一个模块相同。
下一步是指定教师模块内部路由。
以下是
可能的内容
老师-routing.module.ts
以下是教师模块的示例路线
const routes: Routes = [
{path: '', component: TeacherComponent, children: [
{path: '', component: TeacherComponent,data: {title: "Teachers"}},
{path: 'Home', component:HomeComponent, data: {title: "Home"}},
{path: 'Register', component:RegisterComponent, data: {title:
"Register"}},
]
}
]
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class TeacherRoutingModule{}
延迟加载方式Angular v8, v9 and Up
https://angular.io/guide/lazy-loading-ngmodules
// In app module route
{
path: 'your-path',
loadChildren: () => import('./path-to/your.module').then(m => m.YourModule)
}
// in your module
const yourRoutes: Routes = [
{ path: '', component: YourComponent }
];
export const yourRouting = RouterModule.forChild(yourRoutes);
@NgModule({
imports: [
yourRouting
],
declarations: [
YourComponent
]
})
export class YourModule{
}
懒加载方式Angularv7、v6及以下
// In app module route
{
path: 'your-path',
loadChildren: 'app/your.module#YourModule'
}
// in your module
const yourRoutes: Routes = [
{ path: '', component: YourComponent }
];
export const yourRouting = RouterModule.forChild(yourRoutes);
@NgModule({
imports: [
yourRouting
],
declarations: [
YourComponent
]
})
export class YourModule{
}
不是懒加载方式
只需在主模块中导入YourModule
,如果路由不是延迟加载,它就可以工作。
@NgModule({
imports: [
BrowserModule,
FormsModule,
YourModule,
routing
],
declarations: [
AppComponent
],
providers: [
appRoutingProviders
],
bootstrap: [ AppComponent ]
})
export class AppModule {
}
花点时间阅读路由文档https://angular.io/guide/router
检查这个答案
我正在构建简单的 angular 应用程序。有两个模块 student 和 teacher。 这是我的项目组织方式。
首先当用户进入应用程序时,我让他选择他是老师还是学生。
根据他的用户将被重定向到相应的模块。
import {NgModule} from '@angular/core';
import {Routes, RouterModule} from '@angular/router';
import { StudentModule } from './student/student.module';
import { TeacherModule } from './teacher/teacher.module';
import { HomeComponent } from './home/home.component';
const routes: Routes = [
{
path: '',
component: HomeComponent
},
{
path: 'student',
loadChildren: () => StudentModule
},
{
path: 'teacher',
loadChildren: () => TeacherModule
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {
}
这是我的 app.routing.ts
文件。
我的问题是当我重定向到我想在这些模块中的组件之间路由的模块时。我应该在每个模块中添加另一个 <router-outlet>
还是我可以用 AppModule
中唯一的 <router-outlet>
来做到这一点?
如果我应该添加另一个 <router-outlet>
我应该如何为这些模块编写我的路由器 class。
是的,您需要为各个模块定义路由,并在您需要提供的模块组件文件中提供
下面应该是文件结构
- Teacher
-teacher.component.html --> here you should put <router-outlet>
-techer-routing.module.ts --> here you need to define routes for teacher module
-teacher.module.ts --> import techer-routing.module here
-Logincomponent
-login.component.html
-login.component.ts
-Homecomponent
-home.component.html
-home.component.ts
与学生的另一个模块相同。
下一步是指定教师模块内部路由。 以下是
可能的内容老师-routing.module.ts
以下是教师模块的示例路线
const routes: Routes = [
{path: '', component: TeacherComponent, children: [
{path: '', component: TeacherComponent,data: {title: "Teachers"}},
{path: 'Home', component:HomeComponent, data: {title: "Home"}},
{path: 'Register', component:RegisterComponent, data: {title:
"Register"}},
]
}
]
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class TeacherRoutingModule{}
延迟加载方式Angular v8, v9 and Up
https://angular.io/guide/lazy-loading-ngmodules
// In app module route
{
path: 'your-path',
loadChildren: () => import('./path-to/your.module').then(m => m.YourModule)
}
// in your module
const yourRoutes: Routes = [
{ path: '', component: YourComponent }
];
export const yourRouting = RouterModule.forChild(yourRoutes);
@NgModule({
imports: [
yourRouting
],
declarations: [
YourComponent
]
})
export class YourModule{
}
懒加载方式Angularv7、v6及以下
// In app module route
{
path: 'your-path',
loadChildren: 'app/your.module#YourModule'
}
// in your module
const yourRoutes: Routes = [
{ path: '', component: YourComponent }
];
export const yourRouting = RouterModule.forChild(yourRoutes);
@NgModule({
imports: [
yourRouting
],
declarations: [
YourComponent
]
})
export class YourModule{
}
不是懒加载方式
只需在主模块中导入YourModule
,如果路由不是延迟加载,它就可以工作。
@NgModule({
imports: [
BrowserModule,
FormsModule,
YourModule,
routing
],
declarations: [
AppComponent
],
providers: [
appRoutingProviders
],
bootstrap: [ AppComponent ]
})
export class AppModule {
}
花点时间阅读路由文档https://angular.io/guide/router
检查这个答案