多个模块和角度5中的布线 [英] Multiple modules and routing in angular 5

查看:75
本文介绍了多个模块和角度5中的布线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我在项目中使用多个模块时如何正确设置路由吗?我有app.module和courses.module,其中声明了一些组件.我想知道如何连接模块并在courses.module中正确编辑路由,即共享路由:"/courses/list"和"/courses/detail"

can somebody tell me how correct to set up routing when using multiple modules in my project? I have app.module and courses.module with some components declared in. I want to know how to connect modules and edit properly routing in courses.module, thats share routes: "/courses/list" and "/courses/detail"

app.routing.module.ts

app.routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

//import { CoursesRoutingModule } from './components/courses/courses-routing.module';

const routes: Routes = [
  {
    path: 'courses',
    loadChildren: './components/courses/courses-routing.module#CoursesRoutingModule' 
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

app.component.html

app.component.html

<h1>App.component</h1>
<p>
    <button routerLink="/">HOME</button>
    <button routerLink="/courses">KURSY</button>
</p>
<router-outlet></router-outlet>

这是课程的组成部分:

courses.routing.module.ts

courses.routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { CoursesComponent } from './courses.component';
import { CoursesDetailComponent } from './components/courses-detail/courses-detail.component';
import { CoursesListComponent } from './components/courses-list/courses-list.component';

const routes: Routes = [
//  {
//    path: 'courses',
//    loadChildren: '/src/app/components/courses/courses.module'
//  }
//  ,
//  {
//    path: 'courses/list',
//    component: CoursesListComponent,
//    outlet: 'courseslist'
//  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class CoursesRoutingModule { }

courses.component.html:

courses.component.html:

<p>
    <button routerLink="/">HOME</button>
    <button routerLink="/courses/list">list</button>
    <button routerLink="/courses/detail">detail</button>
</p>

推荐答案

编辑08/2019

使延迟加载模块语法适应于angular 8中引入的新的强类型语法.

Adapted the lazy load module syntax to the newly, strong-typed syntax introduced in angular 8.

编辑02/2020:

此构造对于Angular 9仍然有效.

This construct is still valid for Angular 9.

解决方案

这是我的方法:

app.module.ts

import {ModuleRouting} from './app.routing';

@NgModule({
    declarations: [
        AppComponent,
    ],
    imports: [
        ModuleRouting,
        SubmoduleModule         
    ]
    bootstrap: [AppComponent]
})
export class AppModule {
}

app.routing.ts

import {Routes, RouterModule} from '@angular/router';
import {ModuleWithProviders} from '@angular/core';

const routes: Routes = [
    {path: 'submodule', loadChildren: () => import('app/submodule/submodule.module').then(m => m.SubmoduleModule)}
];

export const ModuleRouting: ModuleWithProviders = RouterModule.forRoot(routes);

submodule.module.ts

import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {ModuleRouting} from './submodule.routing';

@NgModule({
    imports: [
        CommonModule,
        ModuleRouting
    ],
    declarations: [
        //...
    ]
})
export class SubmoduleModule {
}

submodule.routing.ts

import {RouterModule, Routes} from '@angular/router';
import {ModuleWithProviders} from '@angular/core';

const routes: Routes = [
    {
        path: '',
        component: SomeComponent,
    },
    {
        path: 'other',
        component: SomeOtherComponent,
    }
];

export const ModuleRouting: ModuleWithProviders = RouterModule.forChild(routes);

您还可以使用角度cli 和然后调整它们: (cd到每个命令之前应在其中创建文件的目录)

You can also generate the module and routing files using the angular cli and then adapt them: (cd to the directory where the files should be created before every command)

ng g m module --routing
ng g m submodule --routing

这篇关于多个模块和角度5中的布线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆