简体   繁体   English

在Angular 2中,私有组件(未导出的组件)的用途是什么

[英]In Angular 2, what is the use of private components (components not exported)

I understand that by exporting the components, a module can make them available to use in any modules those import it. 我知道通过导出组件,模块可以使它们可用于导入该组件的任何模块。 But I am just wondering what would be the "useful" scenario where these components are not exported (or kept as private/local), but are just declared in it's parent module. 但是我只是想知道什么是“有用的”场景,其中这些组件不导出(或保存为私有/本地),而是在其父模块中声明。

There's a methodology called lazy loading in Angular (as well as other forms of) development. 在Angular(以及其他形式的)开发中,有一种称为延迟加载的方法。 The idea would be that each route, or family of routes would be their own module, with all of the components declared in the module. 这样的想法是,每条路线或路线系列将是它们自己的模块,并且所有组件都在模块中声明。 You would not export the components. 您不会导出组件。

Instead where you configure your app routing, instead of passing a component for a route you would import the children of the Module you created for that route. 而是在哪里配置应用程序路由,而不是为路由传递组件,而是导入为该路由创建的模块的子级。

However some assets may need to be shared across the different routes. 但是,可能需要在不同的路线之间共享某些资产。 Maybe that would be a certain modal, or a navbar. 也许那将是某种模式,或者是导航栏。 Those components would be included in a module typically referred to as a SharedModule , and they would be exported from that module. 这些组件将包含在通常称为SharedModule的模块中,并且将从该模块中导出。 The SharedModule would then be added as an import, and then your isolated modules you created for your routing would have access to those shared components (and likely pipes as well). 然后将SharedModule作为导入添加,然后为路由创建的隔离模块将可以访问那些共享组件(以及可能的管道)。

I hope that paints a decent picture as to how/why you would use modules, import modules, export components and what not. 我希望能对如何/为什么使用模块,导入模块,导出组件以及不使用的原因有一个不错的了解。 If you're interested in seeing what I mean you can take a dig through this codebase , specifically take a look at the file src/app/app-routing.module.ts for routing to modules, src/app/shared/shared.module.ts for exported components and what not, and maybe src/app/home/home.module.ts which will show you how I bring in the SharedModule but do not export anything from that module. 如果您有兴趣了解我的意思,可以深入了解此代码库 ,尤其是查看用于路由到模块src/app/shared/shared.module.ts的文件src/app/app-routing.module.ts src/app/shared/shared.module.ts用于导出组件的src/app/shared/shared.module.ts以及不用于导出组件的src/app/home/home.module.ts ,它将向您展示如何引入SharedModule但不从该模块导出任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM