简体   繁体   English

为什么在索引文件中会这样导出呢?

[英]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.

相关问题 为什么我不能在index.js中使用导出的路由 - Why I can not use exported routes in index.js 有没有办法从 JavaScript 文件中获取所有导出的名称? - Is there any way to get all the exported names from a JavaScript file? 为什么在导出到另一个文件时 onSnapshot 侦听器不起作用 - Why isn't onSnapshot listener working when exported to another file 位于app.js中的服务器端对象标记为未在我的index.ejs文件中导出 - Server side object located in app.js marked as not exported in my index.ejs file Webpack 在使用索引文件导出和导入模块时不会拆分巨大的供应商文件 - Webpack doesn't split a huge vendor file when modules are exported and imported using index files npm 从当前文件导出的相同 index.js 导入 - npm import from same index.js that the current file is exported from 在导出的同一文件中使用导出的功能 - Using an exported function in the same file it's exported 在导出的文件中填充变量 - Fill variable in exported file 为什么导入文件可以访问该文件的某些部分,但不能访问未导出的内容? - Why can importing a file let some parts of that file be accessed but not things which aren't exported? 返回导出模块的最佳方式 - Best way to return an exported module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM