简体   繁体   English

Angular 模块之间的路由

[英]Routing between Modules in Angular

I'm building simple angular application.我正在构建简单的角度应用程序。 There are two modules as student and teacher .学生老师两个模块。 Here is how my project organized.这是我的项目的组织方式。

项目结构

First when user enter to the application i let him to choose whether he is an teacher or student.首先,当用户进入应用程序时,我让他选择他是老师还是学生。

Depending on what he user will be redirected in to the corresponding module.根据用户将被重定向到相应模块的内容。

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 {


  } 

Here is my app.routing.ts file.这是我的app.routing.ts文件。

My problem us when i redirected into the module i want to route between component in those module.当我重定向到我想在这些模块中的组件之间路由的模块时,我的问题是我们。 Should i add another <router-outlet> into each modules or can i do that with the only <router-outlet> in AppModule .我应该在每个模块中添加另一个<router-outlet>还是可以使用AppModule唯一的<router-outlet>AppModule

If i should add another <router-outlet> how should i write my router class for those modules.如果我应该添加另一个<router-outlet>我应该如何为这些模块编写我的路由器类。

Lazy loading way Angular v8, v9 and Up Angular v8、v9 及更高版本的延迟加载方式

https://angular.io/guide/lazy-loading-ngmodules 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{
}

Lazy loading way Angular v7, v6 and down Angular v7、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{
}

Not lazy loading way不是懒加载方式

Just import the YourModule in the main module and it will work if the routes are not lazily loaded.只需在主模块中导入YourModule ,如果路由没有延迟加载,它就会工作。

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    YourModule,
    routing
  ],
  declarations: [
    AppComponent
  ],
  providers: [
    appRoutingProviders
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule {
}

Take some time to read rout documentation https://angular.io/guide/router花一些时间阅读路由文档https://angular.io/guide/router

Check this answer https://stackoverflow.com/a/39284277/6786941检查这个答案https://stackoverflow.com/a/39284277/6786941

Yes, you need to define routing for individual modules and in the module component file you need to provide是的,您需要为各个模块定义路由,并在您需要提供的模块组件文件中

Below should be the file structure下面应该是文件结构

- 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

Same as another module for students.与学生的另一个模块相同。

Next step is to specify teacher module internal routes.下一步是指定教师模块内部路由。 below are the probable content of以下是可能的内容

teacher-routing.module.ts教师路由.module.ts

Below are the sample routes for teacher module以下是教师模块的示例路线

 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{}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM