2017-08-01 85 views
1

我的文件夾結構如下:如何從外面/組件導入文件中反應

enter image description here

在我App.Js(這是組件文件夾下),我有:

import variables from '/src/EnvVariables/variables.json'; 

但是,我得到一個錯誤:

You attempted to import /src/EnvVariables/variables.json which falls 
outside of the project src/ directory. Relative imports outside of 
src/ are not supported. You can either move it inside src/, or 
add a symlink to it from project's node_modules/. 

我也試過:

import variables from './src/EnvVariables/variables.json'; 
import variables from '/src/EnvVariables/variables.json'; 
import variables from '/src/EnvVariables/variables.json'; 
import variables from '/EnvVariables/variables.json'; 
import variables from './EnvVariables/variables.json'; 

全部或者給出上述錯誤,或者說「無法解析文件」。

正如他們所說,該文件位於/ src下的文件夾中。但它仍然告訴我它必須在/ src /目錄下。我怎樣才能導入我的variables.json文件?我不想把它放在「組件」下面,我更願意更好地組織它。

+0

您正在使用的WebPack第一個解決方案,可以爲您在問題 –

回答

3

您是否嘗試導入相對路徑? (一個或多個../段)

import variables from "../EnvVariables/variables.json" 

當然,如果你願意,你可以設置https://webpack.js.org/configuration/resolve/絕對路徑,但我相信,第一,你應該嘗試

+0

中添加該文件,非常感謝! – joey