簡體   English   中英

ES6模塊:重新導出為對象

[英]ES6 module: re-export as object

我有moduleA導出一些函數:

// moduleA.js
export function f1() {...}
export function f2() {...}

有沒有辦法在moduleB中重新導出moduleA的所有導出並使其看起來像一個對象:

// moduleB.js
export * as a from 'moduleA';  // pseudo code, doesn't work

這樣我就可以用這種方式使用它?

// main.js
import {a} from 'moduleB';
a.f1();
a.f2();

語法尚不支持,但有一個提案

您現在可以使用它與Babel.js或只是做:

import * as a from '...';
export {a};

file1.js

export let uselessObject = {
  con1 : function () {
    console.log('from file1.js')
  }
}

file2.js

import { uselessObject } from './file1.js'

uselessObject.con2 = function(){
  console.log('from file2.js ')
}

export { uselessObject } from './file1.js'

Index.js

import { uselessObject } from './file2.js'
uselessObject.con1()
uselessObject.con2()

產量

from file1.js
from file2.js

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM