代码之家  ›  专栏  ›  技术社区  ›  Buddhika Chathuranga

模块间的角度布线

  •  0
  • Buddhika Chathuranga  · 技术社区  · 6 年前

    我正在构建简单的角度应用程序。有两个模块 学生 老师 .

    Project structure

    首先,当用户进入应用程序时,我让他选择他是教师还是学生。

    取决于用户将被重定向到相应的模块。

    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

    如果我再加一个 <路由器出口>

    2 回复  |  直到 6 年前
        1
  •  2
  •   Amr Ibrahim    6 年前

    延迟加载方式

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

    花点时间阅读rout文档 https://angular.io/guide/router

    https://stackoverflow.com/a/39284277/6786941

        2
  •  1
  •   Parth Lukhi    6 年前

    下面应该是文件结构

    - 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
    

    下一步是指定教师模块内部路由。 以下是

    以下是教师模块的示例路径

     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{}
    
    推荐文章