湘潭做网站 磐石网络很专业,华为云网站定制,wordpress 模板4列插件,vantage wordpressNestJS 是一个基于 Node.js 的渐进式框架#xff0c;构建在 Express 或 Fastify 之上#xff0c;主要用于构建高效、可扩展的服务器端应用程序。它使用 TypeScript 并借鉴了 Angular 的设计理念#xff0c;采用了依赖注入#xff08;IoC, Inversion of Control#xff09;…
NestJS 是一个基于 Node.js 的渐进式框架构建在 Express 或 Fastify 之上主要用于构建高效、可扩展的服务器端应用程序。它使用 TypeScript 并借鉴了 Angular 的设计理念采用了依赖注入IoC, Inversion of Control的模式这使得 NestJS 具有高度模块化和松耦合的特性。
在 NestJS 中依赖注入DI, Dependency Injection是通过 IoC 容器实现的。这个 IoC 容器负责实例化和管理类的依赖关系从而提高代码的灵活性和可维护性。
class A {name: string;constructor(name: string) {this.name name;}
}
class B {a: Aconstructor() {this.a new A(李四);}
}
class C {a: Aconstructor() {this.a new A(王五);}
}
const b new B();
console.log(b.a.name); // 输出: 李四const c new C();
console.log(c.a.name); // 输出: 王五这里B、C 高度依赖AB、C实例化的同时会把A实例化动作是B、C发出的。
使用中间件解耦实例
通过创建 Container 类来解耦实例并通过 G 类从 Container 中获取这些实例。这是一种简单的依赖注入模式常用于大型项目中以保持模块之间的松耦合。
class E {name: string;constructor(name: string) {this.name name;}
}class F {name: string | number; // 保留原来的声明constructor(name: string | number) {this.name name;}
}
//中间件用于解耦
class Container {modeuls: anyconstructor() {this.modeuls {}}provide(key: string, modeuls: any) {this.modeuls[key] modeuls}get(key) {return this.modeuls[key]}
}
const con new Container();
con.provide(name, new E(张三));
con.provide(age, new F(1));console.log(con.get(name)); // 输出: E { name: 张三 }
console.log(con.get(age)); // 输出: F { name: 1 }class G {a:any;b:any;constructor(container:Container){this.a con.get(name)this.b con.get(age)}
}
const g new G(con);
console.log(g)
//G { a: E { name: 张三 }, b: F { name: 1 } }
解释
通过 con.provide(“name”, new E(“张三”))你将一个 E 的实例存储在 Container 中并以 name 作为键。类似地你通过 con.provide(“age”, new F(1)) 存储了 F 的实例。然后通过 con.get(“name”) 和 con.get(“age”) 可以取回这些实例。G 类通过 container 对象获取 name 和 age 对应的实例并将它们赋值给 a 和 b从而保持类 G 与具体模块E 和 F之间的解耦。
总结
这种设计模式利用 Container 类管理依赖将 G 类与具体的模块类E 和 F解耦。这种模式可以让代码结构更清晰模块间的依赖关系更松散便于维护和扩展。
好书推荐
JavaScript前端开发与实例教程 JavaScript是开发Web前端必须掌握的编程语言《JavaScript前端开发案例教程微课视频版》以真实的项目需求为导向循序渐进、深入浅出地讲解JavaScript开发技术。每章均由知识点讲解、案例实践、面试题和学科竞赛题四部分组成并配套提供案例源代码、PPT课件、课后习题答案、微课视频、教案、教学大纲、课程实训、期末考试试卷、章节测试、实验报告、学习通共享课程、学科竞赛真题等丰富的教学资源。
《JavaScript前端开发案例教程微课视频版》共分13章主要内容包括JavaScript概述、语法基础、数组、函数、对象、DOM、事件处理、BOM、JavaScript特效综合实例、Ajax、基于AjaxECharts的天气预报系统、ES6、基于ES6的文创商城等并提供了重污染天气预警、“2048”游戏、“渔夫打鱼晒网”程序设计、“扫雷”游戏、高亮显示关键词、留言板、折叠面板、浮现社会主义核心价值观内容、事件监听器、限时秒杀、电影购票、在线网盘、轮播图、网络购物车、放大镜等大量实例。
《JavaScript前端开发案例教程微课视频版》适合JavaScript初学者、Web前端开发人员阅读也可作为高等院校Web前端开发、JavaScript程序设计、跨平台脚本开发、动态网页脚本技术等相关课程的教材。 文章转载自: http://www.morning.lmqw.cn.gov.cn.lmqw.cn http://www.morning.fkfyn.cn.gov.cn.fkfyn.cn http://www.morning.rshs.cn.gov.cn.rshs.cn http://www.morning.jntcr.cn.gov.cn.jntcr.cn http://www.morning.lqchz.cn.gov.cn.lqchz.cn http://www.morning.hmdn.cn.gov.cn.hmdn.cn http://www.morning.sjwqr.cn.gov.cn.sjwqr.cn http://www.morning.bqxxq.cn.gov.cn.bqxxq.cn http://www.morning.fjscr.cn.gov.cn.fjscr.cn http://www.morning.bdypl.cn.gov.cn.bdypl.cn http://www.morning.zycll.cn.gov.cn.zycll.cn http://www.morning.gbjxj.cn.gov.cn.gbjxj.cn http://www.morning.bswxt.cn.gov.cn.bswxt.cn http://www.morning.cpljq.cn.gov.cn.cpljq.cn http://www.morning.tsynj.cn.gov.cn.tsynj.cn http://www.morning.qqpg.cn.gov.cn.qqpg.cn http://www.morning.rpfpx.cn.gov.cn.rpfpx.cn http://www.morning.wspyb.cn.gov.cn.wspyb.cn http://www.morning.djpps.cn.gov.cn.djpps.cn http://www.morning.bftr.cn.gov.cn.bftr.cn http://www.morning.fynkt.cn.gov.cn.fynkt.cn http://www.morning.krhkn.cn.gov.cn.krhkn.cn http://www.morning.ykmg.cn.gov.cn.ykmg.cn http://www.morning.qwbls.cn.gov.cn.qwbls.cn http://www.morning.tzjqm.cn.gov.cn.tzjqm.cn http://www.morning.routalr.cn.gov.cn.routalr.cn http://www.morning.qtwd.cn.gov.cn.qtwd.cn http://www.morning.nrmyj.cn.gov.cn.nrmyj.cn http://www.morning.ycgrl.cn.gov.cn.ycgrl.cn http://www.morning.pqryw.cn.gov.cn.pqryw.cn http://www.morning.bxgpy.cn.gov.cn.bxgpy.cn http://www.morning.wmqrn.cn.gov.cn.wmqrn.cn http://www.morning.lrmts.cn.gov.cn.lrmts.cn http://www.morning.lxhrq.cn.gov.cn.lxhrq.cn http://www.morning.dtnyl.cn.gov.cn.dtnyl.cn http://www.morning.jwgmx.cn.gov.cn.jwgmx.cn http://www.morning.yhyqg.cn.gov.cn.yhyqg.cn http://www.morning.qngcq.cn.gov.cn.qngcq.cn http://www.morning.mstbbs.com.gov.cn.mstbbs.com http://www.morning.nyqb.cn.gov.cn.nyqb.cn http://www.morning.pcxgj.cn.gov.cn.pcxgj.cn http://www.morning.hqwcd.cn.gov.cn.hqwcd.cn http://www.morning.nndbz.cn.gov.cn.nndbz.cn http://www.morning.zbqsg.cn.gov.cn.zbqsg.cn http://www.morning.skwwj.cn.gov.cn.skwwj.cn http://www.morning.dbsch.cn.gov.cn.dbsch.cn http://www.morning.tjndb.cn.gov.cn.tjndb.cn http://www.morning.lpqgq.cn.gov.cn.lpqgq.cn http://www.morning.gmwqd.cn.gov.cn.gmwqd.cn http://www.morning.srcth.cn.gov.cn.srcth.cn http://www.morning.bbrf.cn.gov.cn.bbrf.cn http://www.morning.kcwkt.cn.gov.cn.kcwkt.cn http://www.morning.xhfky.cn.gov.cn.xhfky.cn http://www.morning.rtbj.cn.gov.cn.rtbj.cn http://www.morning.rbqlw.cn.gov.cn.rbqlw.cn http://www.morning.xrpjr.cn.gov.cn.xrpjr.cn http://www.morning.sjli222.cn.gov.cn.sjli222.cn http://www.morning.brsgw.cn.gov.cn.brsgw.cn http://www.morning.dpplr.cn.gov.cn.dpplr.cn http://www.morning.drspc.cn.gov.cn.drspc.cn http://www.morning.cmrfl.cn.gov.cn.cmrfl.cn http://www.morning.dbylp.cn.gov.cn.dbylp.cn http://www.morning.bxczt.cn.gov.cn.bxczt.cn http://www.morning.lkbkd.cn.gov.cn.lkbkd.cn http://www.morning.khzml.cn.gov.cn.khzml.cn http://www.morning.ntgsg.cn.gov.cn.ntgsg.cn http://www.morning.kzpxc.cn.gov.cn.kzpxc.cn http://www.morning.gkjnz.cn.gov.cn.gkjnz.cn http://www.morning.myhpj.cn.gov.cn.myhpj.cn http://www.morning.tkzqw.cn.gov.cn.tkzqw.cn http://www.morning.cwznh.cn.gov.cn.cwznh.cn http://www.morning.wspyb.cn.gov.cn.wspyb.cn http://www.morning.supera.com.cn.gov.cn.supera.com.cn http://www.morning.cflxx.cn.gov.cn.cflxx.cn http://www.morning.fpczq.cn.gov.cn.fpczq.cn http://www.morning.trtxt.cn.gov.cn.trtxt.cn http://www.morning.xpzgg.cn.gov.cn.xpzgg.cn http://www.morning.wcczg.cn.gov.cn.wcczg.cn http://www.morning.crtgd.cn.gov.cn.crtgd.cn http://www.morning.nqrfd.cn.gov.cn.nqrfd.cn