ReactJs如何从src文件夹中的同级文件夹导入组件?

时间:2020-06-16 05:23:13

标签: reactjs import

/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';,但没有任何效果。

2 个答案:

答案 0 :(得分:1)

它基于您导出类/函数AppConfig的方式

  1. 说您的类/函数名称为AppConfig,然后将其导出为

const AppConfig =()=> {} 导出默认的AppConfig;

您可以通过以下方式导入AppConfig。

1. For Root.js

import AppConfig from '../constants/AppConfig';

2. For AppLayout.js

import AppConfig from '../../constants/AppConfig';
  1. 说您的类/函数名称为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

一些给您的提示

  • 了解有关根导入的更多信息
  • 使用编辑器为您提供一些功能,在这种情况下为您提供帮助