简体   繁体   English

将组件导出为默认和常规

[英]Exporting component as default and regular

I've seen in numerous sites, but specifically within libraries like Vuetify , there are these index.ts files that are exporting the same component as default and just regular(?) exports.我在许多网站上看到过,但特别是在像Vuetify这样的库中,有这些index.ts文件正在导出与默认相同的组件,并且只是常规(?)导出。

import Component from './Component'

export { Component }
export default Component 

What is the point of exporting the same component twice, albeit, differently?两次导出相同的组件有什么意义,尽管不同?

Actually it just helps the user to import things in the way he or she wants.实际上它只是帮助用户以他或她想要的方式导入东西。 They can choose to import it:他们可以选择导入它:

import { foo } from "./bar"

or或者

import foo from "./bar"

Functionally , there's not much difference.功能上,没有太大区别。 Anything you can do with a default export, you can also do with a named export, with very slight tweaks to the code.您可以对默认导出执行任何操作,也可以对命名导出执行任何操作,只需对代码稍作调整即可。

I think this may be done so that when the library is used in others' codebases, it can be used while conforming to the style of that codebase .我认为可以这样做,以便在其他人的代码库中使用该库时,可以在符合该代码库的样式的同时使用它。

For example, there may be a codebase that tries to always use named imports and exports, in which case they could use import { Component } .例如,可能有一个代码库尝试始终使用命名的导入和导出,在这种情况下,他们可以使用import { Component } Or there might be one which always tries to use default imports and exports.或者可能有一个总是尝试使用默认导入和导出。

Using both named and default exports for the same thing allows more flexibility in how the consumers of the library use it.对同一事物同时使用命名导出和默认导出可以让库的使用者更灵活地使用它。

It might also be done so there's one less thing for consumers to worry about - you don't have to keep in mind "Remember, this library always uses NAMED EXPORTS" or "Remember, this library always uses DEFAULT EXPORTS" - if either will work, that's one less thing to worry about.也可以这样做,这样消费者就可以少担心一件事了——你不必记住“记住,这个库总是使用 NAMED EXPORTS”或“记住,这个库总是使用 DEFAULT EXPORTS”——如果有的话工作,这是一件少担心的事情。

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

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