简体   繁体   English

Javascript 如何使用模块从文件中导入/导出所有内容?

[英]Javascript how to use modules to Import/Export all from file?

With this code I am able to successfully import / access variables and functions when they are individually exported, but I want to be able to export the entire file in a single declaration.使用此代码,我能够在单独导出变量和函数时成功导入/访问它们,但我希望能够在单个声明中导出整个文件。 I've tried a number of variations from here but cannot achieve this.我从这里尝试了许多变体,但无法实现这一点。

script.js脚本.js

import * as testImport from './test.js';

console.log( testImport.testVar ); // Returns 'Test Variable'
console.log( testImport.testFunc() ); // Returns 'Test Function'

test.js测试.js

export let testVar = 'Test Variable';

export function testFunc() {
    return 'Test Function';
}

desired ( without individual 'exports' )需要(没有单独的“出口”)

let testVar = 'Test Variable';

function testFunc() {
    return 'Test Function';
}

export * // ?

There is no such thing like you ask for, because it conflicts diametrically with the intention behind modules.没有像您要求的那样的事情,因为它与模块背后的意图完全冲突。

The whole point of modules is too keep all variables private to the module with the exception of making those explicitly exported available for importing .模块的重点是保持所有变量对模块私有,除了使那些显式导出的变量可用于导入

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

相关问题 如何在同一个 ts 文件中使用所有 import、modules.export 和 export? - How can I use all import, modules.export, and export in the same ts file? 如何将我拥有的所有模块导入一个 ts 文件然后导出? - How to import all modules i have into one ts file then export it? 如何在我的 javascript 文件中从“node_modules”导入或使用已安装的模块 - How to import or use installed modules from 'node_modules' in my javascript file JavaScript 模块导入/导出 - JavaScript modules import / export NodeJs:如何导出Javascript文件中的所有模块和变量? - NodeJs: How to export all the modules and variables in a Javascript file? Javascript 从文件中导入所有模块作为全局变量 - Javascript import all modules from a file as a global variables 如何从javascript / typescript模块文件(导入/导出)访问Vuex存储? - How to access Vuex store from javascript/typescript modules files (import/export)? 如何使用 JavaScript 从同一个文件中导出和导入多个对象? - How to export and import multiple objects from the same file with JavaScript? 如何导入从webpack.config生成的javascript导出文件? - How to import javascript export file generated from the webpack.config? 如何使用Javascript ES6 ES2015模块将常量直接导出/导入到导入模块名称空间? - How do I use Javascript ES6 ES2015 modules to export / import constants directly to the import module namespace?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM