简体   繁体   English

Angular 8个组件

[英]Angular 8 components

What would be the most optimal structure for a business project with many components, (50 approx)?对于具有许多组件(大约 50 个)的业务项目,最佳结构是什么?

That each component has its own module?每个组件都有自己的模块?

src/
├── app
│   ├── app.component.html
│   ├── app.component.scss
│   ├── app.component.ts
│   ├── app.module.ts
│   ├── app-routing.module.ts
│   ├── components
│   │   ├── comp1
│   │   │   ├── comp1.component.ts
│   │   │   ├── comp1.module.ts
│   │   │   └── index.ts
│   │   ├── comp2
│   │   │   ├── comp2.component.ts
│   │   │   ├── comp2.module.ts
│   │   │   └── index.ts
│   │   ├── comp3
│   │   │   ├── comp3.component.ts
│   │   │   ├── comp3.service.ts
│   │   │   ├── comp3.module.ts
│   │   │   └── index.ts
│   ├── views
│   │   ├── admin
│   │   │   ├── admin.module.ts
│   │   │   ├── admin-routing.module.ts
│   │   │   ├── page1 <== Here I show comp1
│   │   │   ├── page2 <== Here I show comp2
│   │   │   ├── page3 <== Here I show comp3

That a module groups all the components?一个模块将所有组件组合在一起? in this case, every time you load the module, will it load all the components in this memory?在这种情况下,每次加载模块时,它会加载这个memory中的所有组件吗?

src/
├── app
│   ├── app.component.html
│   ├── app.component.scss
│   ├── app.component.ts
│   ├── app.module.ts
│   ├── app-routing.module.ts
│   ├── components
│   │   ├── comp1
│   │   │   ├── comp1.component.ts
│   │   ├── comp2
│   │   │   ├── comp2.component.ts
│   │   ├── comp3
│   │   │   ├── comp3.component.ts
│   │   │   ├── comp3.service.ts
│   │   ├── comps.module.ts <=== // group all components in one module
│   │   ├── index.ts
│   ├── views
│   │   ├── admin
│   │   │   ├── admin.module.ts
│   │   │   ├── admin-routing.module.ts
│   │   │   ├── page1 <== Here I show comp1
│   │   │   ├── page2 <== Here I show comp2
│   │   │   ├── page3 <== Here I show comp3

Any suggestion?有什么建议吗?

Yes, In angular when you load a module, all of its components are loaded.是的,在 angular 中,当您加载一个模块时,它的所有组件都会被加载。 You can create feature modules for you different functionalities which you dont want to load at start up.您可以为您不想在启动时加载的不同功能创建功能模块。 (That is done by lazy loading.) (这是通过延迟加载完成的。)

So the structure will be kind of like this所以结构会是这样的

Core module -> All components required at start up核心模块 -> 启动时所需的所有组件

Featured Modules -> Will load on demand later on.特色模块 -> 稍后将按需加载。

In angular9, they have provided the feature to lazy load componenets as well.在 angular9 中,他们也提供了延迟加载组件的功能。 Now you can lazy load components even if the module is loaded.现在,即使模块已加载,您也可以延迟加载组件。 https://johnpapa.net/angular-9-lazy-loading-components/ https://johnpapa.net/angular-9-lazy-loading-components/

There is no strict rule.没有严格的规定。 It depends on the components.这取决于组件。

Usually it is a mix.通常它是混合的。 A component that represents a "page" (eg the top level routes like your admin area) makes a good module.代表“页面”的组件(例如,像您的管理区域这样的顶级路由)是一个很好的模块。 For "smaller" components used only from a single page it makes sense to put them into the same module.对于仅在单个页面中使用的“较小”组件,将它们放入同一个模块中是有意义的。

Other components that are used multiple times from different "page" modules should get into their own module.从不同的“页面”模块多次使用的其他组件应该进入它们自己的模块。

The size of the components is also a consideration.组件的大小也是一个考虑因素。 The larger a module gets the more it may be good to extract smaller modules.模块越大,提取更小的模块可能越好。

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

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