2017-10-19 53 views
0

我需要訪問外部JavaScript文件中的道具。如何從externam js文件訪問道具 - ReactJS

我有一個保存靜態值的JavaScript文件。像

export const SEARCH = { 
    [Data.NAME]: 'search-default', 
    [Data.LABEL]: 'Search', 
    [Data.TYPE]: Types.INPUT_SEARCH, 
    [InputData.PLACEHOLDER]: 'Search' 
}; 

export const SEARCH_MAP = { 
    [Data.NAME]: 'search-map', 
    [Data.LABEL]: 'Search any address in the world', 
    [Data.TYPE]: Types.INPUT_SEARCH, 
    [InputData.PLACEHOLDER]: 'Search any address in the world' 
}; 

我需要訪問道具在運行時動態設置值。基本上我試圖實現多語言實現。

有什麼方法可以在這裏訪問道具嗎?

回答

0

作爲您導出對象的用途。操作員和使用該特定鍵的訪問 例如 const search = require(); //import file name = search.[DATA.NAME] like this u can access values in other js file

+0

是的,我可以在React Component類中導入文件並訪問它。但是我想在這個js中使用道具,這樣就可以根據選擇的語言改變指定的值。我有我的語言字符串在道具中設置,它可以幫助我,如果我可以訪問外部js文件中的道具。 – Joseph

+0

if you same file then y u need props!1 !! –

+0

看起來像我誤傳了。從一個反應Component類,我將引用一個常量(例如Types.INPUT_SEARCH),其中Types.INPUT_SEARCH將被定義在另一個文件中定義。我需要在當時適當的語言設置中爲常量映射值。所以當我檢索值時,我的外部js將返回語言特定的值(在運行時期望從道具中獲得)。 – Joseph