简体   繁体   English

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

[英]ReactJs how to import component from sibling folder inside src folder?

/src
|--components
|  |--Layout
|  |  |--AppLayout.js
|  |  |--Header.js
|  |  |--Content.js
|  |  |--Footer.js
|  |--Root.js
|--constants
|  |-- AppConfig.js
|--index.js

Currently, I have my react js project structured as described above.目前,我的 react js 项目的结构如上所述。 My question is how can I import config from AppConfig.js in the file Root.js(sibling folder inside src folder), and also import it to the AppLayout.js?我的问题是如何从文件 Root.js(src 文件夹中的兄弟文件夹)中的 AppConfig.js 导入配置,并将其导入 AppLayout.js? I have tried 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'; import APPCONFIG from './constants/appConfig'; , import APPCONFIG from './constants/appConfig'; import APPCONFIG from './constants/appConfig'; and import APPCONFIG from 'constants/appConfig';import APPCONFIG from 'constants/appConfig'; but nothing works.但没有任何效果。

It is based on how you export class/function AppConfig它基于您如何导出类/函数 AppConfig

  1. Say your class/function name is AppConfig and you export it like假设您的类/函数名称是 AppConfig 并且您将其导出为

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

You can import AppConfig below way.您可以通过以下方式导入 AppConfig。

1. For Root.js

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

2. For AppLayout.js

import AppConfig from '../../constants/AppConfig';
  1. Say your class/function name is AppConfig and you export it like假设您的类/函数名称是 AppConfig 并且您将其导出为

export const AppConfig = () => {};导出 const AppConfig = () => {};

You can import AppConfig below way.您可以通过以下方式导入 AppConfig。

1. For Root.js

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

2. For AppLayout.js

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

If still not work then please provide way you export your AppConfig class/function如果仍然无法正常工作,请提供导出 AppConfig 类/函数的方式

more details related export and import class/function es6 javascript please check below links有关导出和导入类/函数 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/export

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

from Applayout.js you want to import Appconfig.js从 Applayout.js 你想导入 Appconfig.js

so in Applayout.js import APPCONFIG from '../../constants/AppConfig所以在 Applayout.js import APPCONFIG from '../../constants/AppConfig

Some Tips for you给你的一些提示

  • read more about root import阅读有关根导入的更多信息
  • use editors that gives you some functionalities to help you in this small cases在这种小情况下使用为您提供一些功能的编辑器来帮助您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM