为什么angular-cli创建component/shared/index.ts? [英] Why does angular-cli create component/shared/index.ts?

查看:76
本文介绍了为什么angular-cli创建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.

桶的目标是巩固进口.它将导出共享中包含的项目,以使导入内容在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ünterZö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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆