[英]Why is it exported this way in the index file?
I'm following a nest tutorial and the instructor creates a folder called dtos and inside it creates two dto's (create-user.dto and edit-user.dto).我正在学习嵌套教程,讲师创建了一个名为 dtos 的文件夹,并在其中创建了两个 dto(create-user.dto 和 edit-user.dto)。 Then, create an index file (in the same folder) that contains only the following:
然后,创建一个仅包含以下内容的索引文件(在同一文件夹中):
index.ts:指数.ts:
export * from './create-user.dto';
export * from './edit-user.dto'
I don't understand two things:我不明白两件事:
1-why do you export the dtos from there? 1-为什么要从那里导出 dto? they already export themselves.
他们已经出口了。
2- because it uses exports the dtos directly. 2- 因为它直接使用导出 dto。 Shouldn't I import them first?
我不应该先导入它们吗?
Here is the code of the data: edit-user.dto:这是数据的代码:edit-user.dto:
export class EditUserDto {}
create-user.dto:创建用户.dto:
export class CreateUserDto {}
1-why do you export the dtos from there?
1-为什么要从那里导出 dto? they already export themselves.
他们已经出口了。
It allows for more concise importing.它允许更简洁的导入。 Say your folder structure is:
假设您的文件夹结构是:
top
index
dtos
index
create-user
edit-user
If you import create-user and edit-user into dtos/index, and then export them from dtos/index, you can then import them from the top index with:如果将 create-user 和 edit-user 导入到 dtos/index,然后从 dtos/index 导出它们,则可以使用以下命令从顶部索引导入它们:
import { EditUserDto, CreateUserDto } from './dtos';
This is accessing what dtos/index
exports.这是访问
dtos/index
导出的内容。
Without this - yes, the classes are already exported, but importing them elsewhere takes a few more characters, since you have to navigate the folder structure more.没有这个 - 是的,这些类已经导出,但是将它们导入其他地方需要更多的字符,因为您必须更多地浏览文件夹结构。 From the top's index, you'd need:
从顶部的索引,你需要:
import { EditUserDto } from './dtos/edit-user.dto';
import { CreaetUserDto } from './dtos/create-user.dto';
It's ever so slightly more unwieldy.它变得更加笨拙。 Not a big deal.
没什么大不了的。 Some might prefer the extra boilerplate in order to import more concisely, others might prefer to navigate directly to the nested file location without bothering.
有些人可能更喜欢额外的样板文件以便更简洁地导入,其他人可能更喜欢直接导航到嵌套文件位置而不会打扰。 Either will work just fine.
两者都可以正常工作。
2- because it uses exports the dtos directly.
2- 因为它直接使用导出 dto。 Shouldn't I import them first?
我不应该先导入它们吗?
You can import from a file and export that which you're importing in the same line using that syntax you see.您可以使用您看到的语法从文件导入并导出您在同一行中导入的文件。
export * from 'path'
will take everything path
exports, and export it in the current file as well. export * from 'path'
将获取path
导出的所有内容,并将其也导出到当前文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.