项目目录

创建模块

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 { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core'; import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component'; @NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

app.component.html

<header>
<a [routerLink]="['/user']">用户模块</a>
<a [routerLink]="['/article']">文章模块</a>
<a [routerLink]="['/product']">商品模块</a>
</header>
<router-outlet></router-outlet>

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 { AddressComponent } from './components/address/address.component'; @NgModule({
declarations: [UserComponent, ProfileComponent, AddressComponent],
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 { AddressComponent } from './components/address/address.component'; const routes: Routes = [
{
path:'',component:UserComponent
},
{
path:'profile',component:ProfileComponent
},
{
path:'address',component:AddressComponent
}
]; @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 { }

Angular 自定义模块以及配置路由实现模块懒加载的更多相关文章

  1. angular配置懒加载路由的思路

    前言 本人记性不好,对于别人很容易记住的事情,我愣是记不住,所以还是靠烂笔头来帮我长长记性. 参考文章:https://blog.csdn.net/xif3681/article/details/84 ...

  2. Angular性能优化实践——巧用第三方组件和懒加载技术

    应该有很多人都抱怨过 Angular 应用的性能问题.其实,在搭建Angular项目时,通过使用打包.懒加载.变化检测策略和缓存技术,再辅助第三方组件,便可有效提升项目性能. 为了帮助开发者深入理解和 ...

  3. vue-cli 项目实现路由懒加载

    在vue 单页应用中,如果路由不实现懒加载,那么打包出来的文件将会非常大,加载也会非常慢.vue-router 官网也有相应的介绍,但是具体怎么去实现还是讲的比较模糊的,下面将一步步讲解配置路由懒加载 ...

  4. vue路由懒加载

    大项目中,为了提高初始化页面的效率,路由一般使用懒加载模式,一共三种实现方式.(1)第一种写法: component: (resolve) => require(['@/components/O ...

  5. vue2.x 路由懒加载 优化打包体积

    当打包构建应用时,Javascript 包会变得非常大,影响页面加载.如果我们能把不同路由对应的组件分割成不同的代码块,然后当路由被访问的时候才加载对应组件,这样就更加高效了. 结合 Vue 的异步组 ...

  6. vue+webpack2实现路由的懒加载

    当打包构建应用时,Javascript 包会变得非常大,影响页面加载.如果我们能把不同路由对应的组件分割成不同的代码块,然后当路由被访问的时候才加载对应组件,这样就更加高效了. 结合 Vue 的异步组 ...

  7. 「Vue.js」Vue-Router + Webpack 路由懒加载实现

    一.前言 当打包构建应用时,Javascript 包会变得非常大,影响页面加载.如果我们能把不同路由对应的组件分割成不同的代码块,然后当路由被访问的时候才加载对应组件,这样就更加高效了.结合 Vue ...

  8. 路由懒加载---Vue Router

    一.什么是懒加载? 懒加载也就是延迟加载或者按需加载,即在需要的时候进行加载. 二.为什么在Vue路由中使用懒加载? 像vue这种单页面应用,如果没有应用懒加载,运用webpack打包后的文件将会异常 ...

  9. 18-Angular 自定义模块以及配置路由模块懒加载

    新建项目,新建几个子模块,实现懒加载 用户.商品.文章 新建这三个模块 创建模块的时候后面加 --routing.会自动生成模块的路由文件 先删掉. 重新创建模块带routing 这样就会生成两个文件 ...

随机推荐

  1. es其他常用功能

    es6除了模块化,class,promise,还有一些其他的常用功能. 1.let/const let是定义一个变量,但是这个变量可以重新赋值,const是定义一个常量,这个常量不能重新赋值 let ...

  2. 运输层2——用户数据报协议UDP

    目录 1. UDP概述 2. UDP首部格式 3. UDP首部检验和计算方法 写在前面:本文章是针对<计算机网络第七版>的学习笔记 运输层1--运输层协议概述 运输层2--用户数据报协议U ...

  3. python函数式编程-装饰器

    在代码运行期间动态增加功能的方式,称之为“装饰器”(Decorator). 由于函数也是一个对象,而且函数对象可以赋值给变量,所以通过变量也能调用该函数. >>> def now() ...

  4. 读入 并查集 gcd/exgcd 高精度 快速幂

    ios_base::sync_with_stdio(); cin.tie(); ], nxt[MAXM << ], Head[MAXN], ed = ; inline void added ...

  5. 1. let与const

    1.ES6 新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. var a = []; for (var i = 0;i<10;i++) ...

  6. WEB知识补充 支付宝 支付

  7. vue 项目总结 知识点补充

    1.页面加载后自动执行函数  2.向后端请求数据方法 2-1 :axios 的安装使用 2-2 在组件中使用 2-3 发送请求 2-4 接收数据后渲染 2-5 后端数据渲染 2-6 解决跨域问题 任务 ...

  8. ModelAndView返回Json格式的数据

    第一种方式: 1.自定义类JacksonUtil.java,类中实现tojson方法(即将数据转成json类型): 2.自定义类JsonView 继承 AbstractView 3.xml中配置bea ...

  9. Vue中使用matomo进行访问流量统计的实现

    Vue中使用matomo进行访问流量统计 原文链接 前言 之前做到了一个页面及接口访问流量统计的需求, 然后在网上找了很多帖子,发现有些有的但是写的都不是很详细,所以今天就整理了一下 正文 第一步 首 ...

  10. MySQL Multi-Range Read(MRR 索引多范围查找) 原理与解析

    原理: 如果基表很大,数据没有被缓存,在二级索引上使用范围扫描读取行可能会导致大量的随机磁盘访问.使用Multi-Range Read新特性,mysql可以减少对磁盘的随机读的次数:首先,mysql只 ...