为什么 angular-cli 创建 component/shared/index.ts? [英] Why does angular-cli create component/shared/index.ts?
问题描述
如果我运行这个:
ng g component components/blogs
我明白了
app
+--components
| +--blogs
| | +--shared
| | | +--index.ts // what's this for?
| | +--blogs.component.css
| | +--blogs.component.html
| | +--blogs.component.ts
| | +--blogs.component.spec.ts // unit tests!
| | +--index.ts
其余的我都明白,但是 /blogs/shared/index.ts
有什么用?如果该组件文件夹仅用于组件,为什么组件具有共享文件夹?
I understand the rest, but what is the /blogs/shared/index.ts
for? Why does a component have a shared folder if that component folder is just for the component?
推荐答案
共享目录中的 index.ts
文件的想法被称为桶.
The idea of the index.ts
file in the shared dir is something called a barrel.
它的目标是巩固进口.它将导出 shared 中包含的项目以使 blogs.component.ts
中的导入更干净...
The goal of the barrel it to consolidate imports. It will export the items contained within shared to make the imports in blogs.component.ts
cleaner...
app/components/blogs/shared/blogs.service.ts
export class BlogsService { ... }
app/components/blogs/shared/blog.model.ts
export class Blog { ... }
app/components/blogs/shared/index.ts
export * from './blogs.service';
export * from './blog.model';
app/components/blogs/blogs.component.ts
// without barrel
import { BlogsSerivce } from './shared/blogs.service';
import { Blog } from './shared/blog.model';
// with barrel
import { BlogService, Blog } from './shared';
如果您可以想象,随着您添加更多组件/服务/指令/模型,这会变得更加整合.
And if you can imagine this becomes much more consolidated as you add more components/services/directives/models.
参考您可以在官方风格指南(感谢 Günter Zöchbauer)
REFERENCE You can read about barrels in the official style guide (Thanks to Günter Zöchbauer)
这篇关于为什么 angular-cli 创建 component/shared/index.ts?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!