热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Angular自定义模块以及配置路由实现模块懒加载

项目目录创建模块nggmodulemoduleuser--routingnggmodulemodulearticle--routingnggmodulemoduleproduct-

项目目录

Angular 自定义模块以及配置路由实现模块懒加载

创建模块

ng g module module/user --routing
ng g module module/article --routing 
ng g module module/product --routing

创建组件

ng g component module/user
ng g component module/user/components/profile 
ng g component module/user/components/order
ng g component module/article
ng g component module/article/components/articlelist 
ng g component module/article/components/info
ng g component module/product
ng g component module/product/components/plist
ng g component module/product/components/pinfo

 

1.app.module.ts

import { AppComponent } from './app.component';


//上百个组件 会导致页面加载比较慢 所以要模块化
//引入自定义模块
import { UserModule } from './module/user/user.module';
import { ProductModule } from './module/product/product.module';
import { ArticleModule } from './module/article/article.module';
@NgModule({
  declarations: [
    AppComponent,

  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
  
    UserModule,
    ArticleModule,
    ProductModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

 

app.component.html

app-routing.module.ts

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


const routes: Routes = [
  {

    path:'user',loadChildren:'./module/user/user.module#UserModule'
  },
  {

    path:'article',loadChildren:'./module/article/article.module#ArticleModule'
  },
  {

    path:'product',loadChildren:'./module/product/product.module#ProductModule'
  },

  {

    path:'**',redirectTo:'user'
  }

];

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

 

用户模块user.module.ts

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

import { UserRoutingModule } from './user-routing.module';
import { UserComponent } from './user.component';
import { ProfileComponent } from './components/profile/profile.component';
import { OrderComponent } from './components/order/order.component';


@NgModule({
  declarations: [UserComponent, ProfileComponent, OrderComponent],
  imports: [
    CommonModule,
    UserRoutingModule
  ]
})
export class UserModule { }

 

 user-routing-module.ts

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

import { UserComponent } from './user.component';

import { ProfileComponent } from './components/profile/profile.component';
import { OrderComponent } from './components/order/order.component';

const routes: Routes = [
  {
    path:'',component:UserComponent
  },
  {
    path:'profile',component:ProfileComponent
  },
  {
    path:'address',component:OrderComponent
  }
];

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

 

 其他模块类似配置

自定义模块的父子路由

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


import { ProductComponent } from './product.component';


import { PlistComponent } from './components/plist/plist.component';
import { CartComponent } from './components/cart/cart.component';
import { PcontentComponent } from './components/pcontent/pcontent.component';


const routes: Routes = [

  {
    
    path:'',component:ProductComponent,
    children:[
      {path:'cart',component:CartComponent},
      {path:'pcontent',component:PcontentComponent}
    ]

  },

  {path:'plist',component:PlistComponent}
];

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

 


推荐阅读
author-avatar
手机用户2502910463
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有