2017-04-26 91 views
0

我不能明白爲什麼下面的方法行不通:出口做出反應常數

constants.js

import { createConstants } from '../utils'; 

export default createConstants(
    'LOGIN_REQUEST', 
    'LOGIN_SUCCESS', 
    'LOGIN_FAILURE', 
    'LOGOUT', 
    'FETCH_DATA_REQUEST', 
    'RECEIVE_DATA' 
); 

utils.js

import React from 'react'; 

export function createConstants(...constants) { 
    return constants.reduce((acc, constant) => { 
     acc[constant] = constant; 
     return acc; 
    }, {}); 
} 

接下來我要導入LOGIN_REQUEST例如,作爲還原行動。

import { LOGIN_REQUEST, LOGIN_SUCCESS, LOGIN_FAILURE, LOGOUT } from '../constants'; 

,但我每次都輸入常數越來越undefined。 它只能當我定義是這樣的:

export const LOGIN_REQUEST = 'LOGIN_REQUEST'; 

也許有人有一些想法?

+0

如果你寫了'module.exports = createConstants(...)',它可能會工作。 – Sulthan

回答

0

導出沒有這樣的工作。您可以嘗試類似:

import allConstants from '../constants'; 

然後用一個常數,如:

allConstants.LOGIN_REQUEST 
0

你的第一種方法被稱爲default export。它不起作用,因爲你使用的語法不正確。

MDN export entry,這就是你如何寫一個default export //模塊 「我-module.js」 出口默認功能立方體(X){ 返回X * X * X; }

你的第二種方法被稱爲named export,它的工作原理是因爲它有正確的語法。再次來自MDN:

export const foo = Math.sqrt(2); // exports a constant 

希望它有幫助。