4

如何在 Angular 2 ngmodule 的 app-routing.module 文件中为 loadchildren 提供正确的路径名,我遵循了 Angular 主网站中的 ngmodule 概念,但它没有提供此类信息。我遇到了 app-routing.module 路径的问题,我需要在路径名中指定什么,对于这个问题,延迟加载不起作用。所有文件一次加载一次,有人可以帮忙吗?我在 loadchildrens 路径中想念什么?跟着这个 Angular NgModule

app-routing.module 文件。

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import {DashboardModule} from './dashboard/dashboard.module';
import {VideosModule} from './videos/videos.module';


export const routes: Routes = [
    { path: 'login', component: LoginComponent },
    { path: '', redirectTo: 'home/dashboard', pathMatch: 'full', canActivate: [AuthGuard] },
    {
        path: 'home', component: HomeComponent, canActivate: [AuthGuard],
        children: [
            { path: '', loadChildren: 'app/dashboard/dashboard.module#DashboardModule' },
            { path: 'videos', loadChildren: 'app/videos/videos.module#VideosModule' },

            ]
    },
];

app.module 文件

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {FormsModule, FormGroup, ReactiveFormsModule} from '@angular/forms';
import { CommonModule} from '@angular/common';

import {AppRoutingModule } from './app-routing.module';
import { AppComponent }   from './app.component';
import { AuthGuard } from './auth.guard';
import { AuthenticationService } from './shared/services/authentication.service';
import {LoginComponent} from './login/login.component';

import {SharedModule} from './shared/share.module';

import {DashboardModule} from './dashboard/dashboard.module';
import {VideosModule} from './videos/videos.module';

@NgModule({
    imports: [
        BrowserModule, FormsModule, AppRoutingModule, DashboardModule,
        SharedModule, VideosModule, 
        ReactiveFormsModule, CommonModule
    ],
    declarations: [AppComponent, LoginComponent
    ],
    exports: [],
    providers: [
        AuthGuard,
        AuthenticationService,
          ],
    bootstrap: [AppComponent]
})

export class AppModule { }

视频路由.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import {FileUploadComponent} from './upload_videos/uploadvideo.component';
import {ManageVideosComponent} from './manage_videos/managevideos.component';


 export const routes: Routes = [
      { path: ' ', redirectTo:'fileupload',pathMatch:'full'},
      { path: 'fileupload', component: FileUploadComponent },                         
      { path: 'manage_videos', component: ManageVideosComponent },
  ];
@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class VideosRoutingModule {}

视频.模块文件

import { NgModule }           from '@angular/core';
import { CommonModule }       from '@angular/common';

import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {SharedModule} from '../shared/share.module';
import {VideoFileService} from './services/videofile.service';

import { FileSelectDirective, FileDropDirective } from 'ng2-file-upload';
import {FileUploadModule} from 'ng2-file-upload/ng2-file-upload';
import {ManageVideosComponent} from './manage_videos/managevideos.component';

import {VideosRoutingModule} from './videos-routing.module';


@NgModule({
  imports:      [ VideosRoutingModule,SharedModule,CommonModule,
                  FormsModule,ReactiveFormsModule ,FileUploadModule],
  declarations: [ManageVideosComponent,
                 FileUploadComponent],
  exports:      [ FileSelectDirective,FileDropDirective ],
  providers:    [ VideoFileService ]
})
export class VideosModule { }
4

1 回答 1

3

我找到了正确的解决方案。

我们需要从 app.module.ts 文件中删除视频模块导入模块,除了仪表板模块,因为它首先加载并且我们已经使用 loadChildren 概念在 app.routing.ts 文件中导入了视频模块。所以不需要在其中导入视频模块app.module.ts ,因为它的延迟加载,现在它的延迟加载工作正常,以及路径名,无论你想要什么,你都可以给出,并使用路由器链接调用该路径。只需遵循以下链接 https://devblog.dymel .pl/2016/10/06/lazy-loading-angular2-modules/ 谢谢

于 2017-01-26T07:27:23.407 回答