简体   繁体   English

React 从一个文件夹中导入多个组件

[英]React import multiple components from a folder

I am trying to import 2 components from the components folder.我正在尝试从components文件夹中导入 2 个组件。 Both components' class are export default .两个组件的 class 都是export default

However, I got an error message that I should use the curly braces in my import statement.但是,我收到一条错误消息,提示我应该在导入语句中使用花括号。 However, both components above are not using named export so curly braces is not needed when importing them.但是,上面的两个组件都没有使用named export ,因此在导入它们时不需要花括号。

Why isn't it working?为什么它不起作用? How can I make this work?我怎样才能使这项工作?

Line 4 error第 4 行错误主应用

登录组件

as @casimirth stated above since those components are from different files, even though on the same folder, then you need to import them separately as below正如上面的@casimirth 所说,因为这些组件来自不同的文件,即使在同一个文件夹中,那么您需要分别导入它们,如下所示

import Login from "./components/Login"
import Question from "./components/Question"

But i think i know what you're looking for, being able to import them all on one line right?但我想我知道你在找什么,能够将它们全部导入一行,对吗?

below are couple of the ways以下是几种方法

1.put them all on single file and use exports , since in one file only one component can use export default 1.将它们全部放在一个文件中并使用exports ,因为在一个文件中只有一个组件可以使用export default

// ./components/componentfile.js
export const Login = () => {...
export const Question = () => {...

then where you're using them you can import them as然后在您使用它们的地方,您可以将它们导入为

import {Login, Question} from '.components/componentfile'

if one of the file is exported with default as below如果其中一个文件默认导出如下

const Login = () => {...
export const Question = () => {...
export default Login;

then the using them will be as below那么使用它们将如下所示

import Login , { Question } from './components/componentfile'

2. You wish to keep this two files separately and still import on one line 2.您希望将这两个文件分开保存并仍然导入一行

then you need to add another file in components file, prefered index.js since if you name a directory without specifying a file, index.js is one being called by default那么您需要在组件文件中添加另一个文件,首选 index.js,因为如果您在不指定文件的情况下命名目录,则默认情况下会调用 index.js

So, you components directory will have three files,因此,您的组件目录将包含三个文件,

./components
   -index.js
   -login.js
   -questions.js

then without editing anything on your login.js,questions.js import them on your index.js and export them from it as below然后在您的 login.js 上不编辑任何内容,questions.js 将它们导入您的 index.js 并从中导出它们,如下所示

import Login from './login'
import Question from './question'

export {Login, Question}

then where you're using them you just import them as below然后在你使用它们的地方你只需将它们导入如下

import {Login, Question} from './components' //note with index.js no need to mention //it on import

You can create an index.js file in the components folder that imports and exports all the components, then you will be able to import the components at the same line.您可以在 components 文件夹中创建一个index.js文件来导入和导出所有组件,然后您就可以在同一行导入组件。

This question and answer might help you, and the special part is it can performs both import and export using only one single line of code.这个问题和答案可能会对您有所帮助,特别之处在于它可以仅使用一行代码执行导入和导出。

Try importing them individually:尝试单独导入它们:

import Login from "./components/Login"
import Question from "./components/Question"

Create a new file in folder called "index.js" in your Components folder, then do this below.在您的组件文件夹中的名为“index.js”的文件夹中创建一个新文件,然后在下面执行此操作。

import { Component1 } from "./Component1";
import { Component2 } from "./Component2";

export {
    Component1,
    Component2
}

after that you can import like this..之后你可以像这样导入..

import { Component1, Component2 } from './components';

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

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