相关推荐:《angular教程》
一、angular 内置模块
二、angular 自定义模块
当我们项目比较小的时候可以不用自定义模块。但是当我们项目非常庞大的时候把所有的组 件都挂载到根模块里面不是特别合适。所以这个时候我们就可以自定义模块来组织我们的项 目。并且通过 angular 自定义模块可以实现路由的懒加载。
ng g module mymodule
新建一个 user 模块
ng g module module/user
新建一个 user 模块下的根组件
ng g component module/user
新建一个 user 模块下的 address,order,profile 组件
ng g component module/user/components/address ng g component module/user/components/order ng g component module/user/components/profile
如何在根模块挂载 user 模块呢?
在 app 根组件的模板文件 app.component.html 里 引用 user 组件会报错
需要如下处理才可以被访问
1、在 app.module.ts 引入模块
2、user 模块暴露出 要被外界访问到的组件
3、在根模板 app.component.html 里引入
<app-user></app-user>
如果需要在根组件里直接 使用 app-address 组件,也是需要先在 user 模块 user.module.ts 暴露
/暴露组件 让其他模块里面可以使用暴露的组件/
exports:[usercomponent,addresscomponent]
如何在根模块挂载 product 模块呢?
同上
创建 user 模块下的服务
1、创建
ng g service module/user/services/common
2、在 user 模块引入服务
user.module.ts
配置路由实现模 块懒加载
创建模块:
ng g module module/user --routing ng g module module/article --routing ng g module module/product --routing
创建组件:
ng g component module/userng g component module/user/components/profileng g component module/user/components/orderng g component module/articleng g component module/article/components/articlelist ng g component module/article/components/infong g component module/productng g component module/product/components/plistng g component module/product/components/pinfo
这里先以article为例讲解:
angular配置懒加载
在angular中路由即能加载组件又能加载模块,而我们说的懒加载实际上就是加载模块,目前还没有看到懒加载组件的例子。
加载组件使用的是component关键字
加载模块则是使用loadchildren关键字
1. 在app文件夹下 新建 app-routing.module.ts
内容如下:
import { ngmodule } from '@angular/core';import { routes, routermodule } from '@angular/router';@ngmodule({ imports: [routermodule.forroot(routes)], exports: [routermodule]})export class approutingmodule { }
forroot是用在根模块加载路由配置,
而forchild是用在子模块加载路由配置。
注意:需要在根模板 app.module.ts里导入approutingmodule模块
import { approutingmodule } from './app-routing.module';...imports: [ approutingmodule,]
2. 在子模块里配置路由
在\module\article\article-routing.module.ts里配置路由
import { ngmodule } from '@angular/core'; import { routes, routermodule } from '@angular/router'; // import {articlecomponent} from './article.component'; const routes: routes = [ // { // path:'', // component:articlecomponent // } ]; @ngmodule({ imports: [routermodule.forchild(routes)], exports: [routermodule] }) export class articleroutingmodule { }
也可以在新建项目的时候 就把路由的模块加上,可以省去上面的 配置
在 article模块的 article-routing.module.ts配置路由
.....import {articlecomponent} from './article.component';const routes: routes = [ { path:'', component:articlecomponent }];......
3. 在app的路由模块进行配置路由
const routes: routes = [ { path:'article', //写法一: loadchildren:'./module/article/article.module#articlemodule' //写法二 // loadchildren: () => import('./module/user/user.module').then( m => m.usermodule) }, // { // path:'user',loadchildren:'./module/user/user.module#usermodule' // }, // { // path:'product',loadchildren:'./module/product/product.module#productmodule' // }, { path:'**',redirectto:'article' }];
如果在之前新建模块的时候没有加上–routing,,需要配置模块的路由
product模块
product的路由:module\product\product-routing.module.ts
import { ngmodule } from '@angular/core';import { routes, routermodule } from '@angular/router';import {productcomponent} from './product.component';const routes: routes = [ { path:'', component:productcomponent }];@ngmodule({ imports: [routermodule.forchild(routes)], exports: [routermodule]})export class productroutingmodule { }
product的模块:
module\product\product.module.ts
import { productroutingmodule } from './product-routing.module';imports: [ productroutingmodule ],
user模块
user的路由: \module\user\user-routing.module.ts
import { ngmodule } from '@angular/core';import { routes, routermodule } from '@angular/router';import {usercomponent} from './user.component';const routes: routes = [ { path:'', component:usercomponent }];@ngmodule({ imports: [routermodule.forchild(routes)], exports: [routermodule]})export class userroutingmodule { }
user的模块: \module\user\user.module.ts
import {userroutingmodule} from './user-routing.module'; + imports: [ userroutingmodule + ],
routermodule.forroot() 和 routermodule.forchild()
routermodule对象为提供了两个静态的方法:forroot()和forchild()来配置路由信息。
routermodule.forroot()方法用于在主模块中定义主要的路由信息,routermodule.forchild()与 router.forroot()方法类似,但它只能应用在特性模块中。
即根模块中使用forroot(),子模块中使用forchild()。
配置子路由
1、在商品模块的路由product-routing.module.ts 配置子路由
import { plistcomponent } from './components/plist/plist.component';import { cartcomponent } from './components/cart/cart.component';import { pinfocomponent } from './components/pinfo/pinfo.component';const routes: routes = [ { path:'', component:productcomponent, children:[ {path:'cart',component:cartcomponent}, {path:'pcontent',component:pinfocomponent} ] }, {path:'plist',component:plistcomponent}];
2、在商品模块的模板product.component.html 添加router-outlet
<router-outlet></router-outlet>
3、在页面app.component.html添加菜单,方便跳转
<a [routerlink]="['/product']">商品模块</a><a [routerlink]="['/product/plist']">商品列表</a>
更多编程相关知识,请访问:编程视频!!
以上就是深入了解angular中的模块和懒加载的详细内容。
