2017-04-03 42 views
0

我想在課堂上使用import關鍵字。要導入的數據將是一個簡單的對象如:es6 js導出是否必須導出一個函數?

{ 
    test: 123, 
    anothertest: 321 
} 

我知道我可以寫:

module.exports = { 
    test: 123, 
    anothertest: 321 
} 

然後在課堂上,我可以只要求模塊

let data = require('./modulename') 

但我怎麼能寫一個簡單的出口,所以在我可以只寫

import data from 'modulename' 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import演示瞭如何用函數做到這一點,但不是簡單的對象。

+1

你爲什麼沒有看到[這個MDN頁面](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export)? – Jai

+0

:)你怎麼看? – John

回答

1

像這樣簡單:

const data = { 
    test: 123, 
    anothertest: 321 
}; 

export default data; 

如果你有更多的東西要出口的話,你可以將其導出這樣的:

const data = { 
    test: 123, 
    anothertest: 321 
}; 

const stuff = { 
    test: 464, 
    another: 323 
}; 

export { data, stuff }; 

然後你就可以將其導入這樣的:

import { data, stuff } from './file';