简体   繁体   English

在ReactJS中导入/导出子组件错误

[英]Importing/Exporting child component error in ReactJS

I just simply want to export and import a child component into my rot-directory (App.js) and render it out in the browser, but I get this error message in terminal "Module not found: Error: Cannot resolve 'file' or 'directory'". 我只是想将一个子组件导出并导入我的rot目录(App.js)并在浏览器中将其渲染出来,但是我在终端“未找到模块:错误:无法解析'文件'或”中收到此错误消息'目录'”。 I don't understand what I typed wrong or why I cannot import my child to my App.js. 我不明白我输错了什么或为什么我不能将我的孩子导入我的App.js.

Have tried to solve this problem but with no results. 试图解决这个问题,但没有结果。 I've been testing this in my "App.js" to get a more explicit name but not working: 我一直在我的“App.js”中对此进行测试,以获得更明确的名称但不起作用:

import { ContactsList } from './ContactsList';

I've also tried typing this in my "ContactsList.js" but with no result: 我也试过在我的“ContactsList.js”中输入这个但没有结果:

export default class ContactsList extends React.Component {}

I'am a beginner so excuse me for my knowledge but I really want to learn this and the power of react. 我是一个初学者,请原谅我的知识,但我真的想学习这个和反应的力量。 Please help me for better understanding! 请帮助我更好地理解!

--------App.js--------- -------- --------- App.js

import React from 'react';
import ReactDOM from 'react-dom';
import ContactsList from './ContactsList';


class App extends React.Component {
    render() {
        return (
            <div>
                <h1>Contacts List</h1>
                <ContactsList />
            </div>
        )
    }
}
ReactDOM.render(<App />, document.getElementById('app'));

--------ContactsList.js--------- -------- --------- ContactsList.js

import React from 'react';
import ReactDOM from 'react-dom';

class ContactsList extends React.Component {
    render() {
        return (
            <ul>
                <li>Joe 555 555 5555</li>
                <li>Marv 555 555 5555</li>
            </ul>
        )
    }
}
export default ContactsList;

--------webpack.config.js--------- -------- --------- webpack.config.js

module.exports = {
    entry: './src/App.js',
    output: {
        path: __dirname,
        filename: 'app.js'
    },
    module: {
        loaders: [{
            test:/\.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel',
            query: {
                presets: ['es2015', 'react']
            }
        }]
    }
};

In your ContactsList.js file, use a <div> to wrap the <ul> 在您的ContactsList.js文件中,使用<div>包装<ul>

Also in your webpack config file. 也在你的webpack配置文件中。 Can you try to use loader : "babel-loader" instead of loader: 'babel' (Don't forget to install the babel-loader package) 你能尝试使用装载机:“babel-loader”而不是装载机:'babel' (不要忘记安装babel-loader包)

Also remove the query part and try to create a separate .babelrc file with the following settings: 同时删除查询部分并尝试使用以下设置创建单独的.babelrc文件:

{
    "presets" : [
        "react",
        "es2015"
    ]
}

Hope this can solve your problem 希望这可以解决您的问题

According to es6 module mechanism the default module should be imported without {} 根据es6模块机制,默认模块应该在没有{}的情况下导入

import ContactsList  from './ContactsList';

and export like 和出口一样

export default class ContactsList extends React.Component {}

But I guess you are trying babel on .jsx extension however it seams you are using ContactsList.js 但我猜你正在尝试使用.jsx扩展名的babel,但它接缝你正在使用ContactsList.js

Just change the to .jsx to .js in 只需将.jsx更改为.js即可

--webpack.config.js --webpack.config.js

module.exports = {
    entry: './src/App.js',
    output: {
        path: __dirname,
        filename: 'app.js'
    },
    module: {
        loaders: [{
            test:/\.js$/,
            exclude: /node_modules/,
            loader: 'babel',
            query: {
                presets: ['es2015', 'react']
            }
        }]
    }
};

Hope it works 希望它有效

You need to do some changes on webpack.config.js file. 您需要对webpack.config.js文件进行一些更改。 first replace 首先替换

    test:/\.jsx?$/,

with

    test: /\.(js|jsx)$/,

Secondly import modules as follows 其次导入模块如下

import ContactsList  from 'path-of-the-file';

But you need to provide the actual path. 但是你需要提供实际的路径。 to get the path correct there are many plugins available depending on the text editors we use. 为了使路径正确,有许多插件可用,具体取决于我们使用的文本编辑器。 i am using https://github.com/sagold/FuzzyFilePath 我正在使用https://github.com/sagold/FuzzyFilePath

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

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