/src
|--components
| |--Layout
| | |--AppLayout.js
| | |--Header.js
| | |--Content.js
| | |--Footer.js
| |--Root.js
|--constants
| |-- AppConfig.js
|--index.js
目前,我已经按照上述方式构建了我的react js项目。我的问题是如何从文件Root.js(src文件夹内的同级文件夹)中的AppConfig.js导入配置,并将其也导入AppLayout.js?我已经尝试过import APPCONFIG from '../../constants/appConfig';
,import APPCONFIG from '../constants/appConfig';
,import APPCONFIG from './constants/appConfig';
,import APPCONFIG from './constants/appConfig';
和import APPCONFIG from 'constants/appConfig';
,但没有任何效果。
答案 0 :(得分:1)
它基于您导出类/函数AppConfig的方式
const AppConfig =()=> {} 导出默认的AppConfig;
您可以通过以下方式导入AppConfig。
1. For Root.js
import AppConfig from '../constants/AppConfig';
2. For AppLayout.js
import AppConfig from '../../constants/AppConfig';
export const AppConfig =()=> {};
您可以通过以下方式导入AppConfig。
1. For Root.js
import {AppConfig} from '../constants/AppConfig';
2. For AppLayout.js
import {AppConfig} from '../../constants/AppConfig';
如果仍然无法正常工作,请提供导出AppConfig类/函数的方式
有关导出和导入类/函数es6 javascript的更多详细信息,请检查以下链接
https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
答案 1 :(得分:0)
从Applayout.js您要导入Appconfig.js
如此在Applayout.js中
import APPCONFIG from '../../constants/AppConfig