ES6 how to export all item from one file

2019-02-27 08:57发布

问题:

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.