ES6 how to export all item from one file

随声附和 提交于 2019-12-02 04:20:02

问题


I want to export all methods of a file from another file.

currently I am doing this, and it works. How can I merge below two into 1 export expression

import  * as db  from './web/query';
export default db;

I tried below written 1 line exports but all failed

export *   from './web/query';  //==error
export *  as default  from './web/query';  //==error
export *  as {default}  from './web/query';  //==error
export from from './web/query'; //== error
export default from './web/query'; //== error

Error means

import db from '../db/index';

db is undefined here. However the the first methods works

Inside of file './web/query' looks like

export function foo(){}
export function baar(){}

回答1:


You cannot in ES2016. To create a module namespace object, you need to give it an identifier (like db) in your current module scope, and then re-export that. There's no way around it.

There is however a stage 1 proposal to add the export * as default from … syntax you were trying.




回答2:


How can I merge below two into 1 export expression

You cannot.

ES2015 (and ES2016) does not provide a syntax that would allow you to import all the named exports from a file and export the object (with those as its properties) as default in a single statement.



来源:https://stackoverflow.com/questions/41236920/es6-how-to-export-all-item-from-one-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!