简体   繁体   English

ES6导入默认导出内

[英]ES6 Imports inside Export default

I'm currently migrating the whole code of a NodeJS application from ES5 to ES6/7. 我目前正在将NodeJS应用程序的整个代码从ES5迁移到ES6 / 7。

I'm having trouble when it comes to imports : 我在进口方面遇到麻烦:

First, I understood that making an import directly call the file. 首先,我了解进行导入可以直接调用该文件。 For example : 例如 :

 import moduleTest from './moduleTest'; 

This code will go into moduleTest.js and execute it. 这段代码将进入moduleTest.js并执行。

So, the real question is about this code : 因此,真正的问题是关于此代码的:

 import mongoose from 'mongoose'; import autopopulate from 'mongoose-autopopulate'; import dp from 'mongoose-deep-populate'; import { someUtils } from '../utils'; const types = mongoose.Schema.Types; const deepPopulate = dp(mongoose); export default () => { // DOES SOMETHING USING types AND deepPopulate return someThing; }; export const anotherModule = () => { // ALSO USE types and deepPopulate }; 

Is this a good practice to have types and deepPopulate declared outside of the two exports ? 在两个出口之外声明typesdeepPopulate是否是一个好习惯? Or should I declare them in each export ? 还是应该在每次出口中声明它们?

The reason of this question is that I'm having a conflict due to this practice (to simplify, let's say that dp(mongoose) will call something that is not declared yet) 这个问题的原因是由于这种做法,我遇到了冲突(为简化起见,假设dp(mongoose)将调用尚未声明的内容)

You can only have one 'default' export to a module, or you can have multiple 'named' exports per module. 您只能对一个模块进行一次“默认”导出,或者每个模块可以进行多次“命名”导出。 Take a look at the following for a good description of handling exports in ES6: ECMAScript 6 Modules: The Final Syntax 请看以下有关在ES6中处理导出的详细说明: ECMAScript 6模块:最终语法

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

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