简体   繁体   English

React create app - 如何导入嵌套组件?

[英]React create app - How do you import a nested component?

I am using React create app and I understand how to import a component into my main app which is called app.js.我正在使用 React 创建应用程序,并且我了解如何将组件导入到我的主应用程序中,该应用程序称为 app.js。 What I do not understand is how I import a component in a component which is then imported in app.js (nested component).我不明白的是我如何在组件中导入一个组件,然后在 app.js(嵌套组件)中导入。 In my app.js file, I have a component called Portfolio.在我的 app.js 文件中,我有一个名为 Portfolio 的组件。 In my portfolio.js file, I have a component called Language.在我的portfolio.js 文件中,我有一个名为Language 的组件。 Is it even possible to nest components with React create app?甚至可以使用 React create app 嵌套组件吗? Because it does not seem to work.因为它似乎不起作用。

This is what I tried to do:这就是我试图做的:

In my app.js file :在我的 app.js 文件中:

import React from "react";
import logo from "./logo.svg";
import "./App.css";
import Portfolio from './components/portfolio';    
import "./css/styles.css"


function App() {
  return (
    <div className="App">
      <header>
      <h1>My beautiful title</h1>

      </header>
      <Portfolio />
    </div>
  );
}

export default App;

In my portfolio.js file :在我的投资组合.js 文件中:

 import React, {Component} from 'react';
import Language from './components/language';
class Portfolio extends Component {

      render() {

        return (
          <div className="portfolio">
         <Language/>
            <h1>My protfolio</h1>


          </div>
        );
      }
    }

    export default Portfolio;

And in language.js I have:在 language.js 我有:

import React, {Component} from 'react';

class Language extends Component {
  render() {
    return(
<div>Switch language</div>
      );
  }
}

export default Language;

In my browser, I keep getting the following error:在我的浏览器中,我不断收到以下错误:

"./src/components/portfolio.js Module not found: Can't resolve './components/language' in 'C:\\Users\\mluce\\exercice-react\\src\\components'" “./src/components/portfolio.js Module not found: Can't resolve './components/language' in 'C:\\Users\\mluce\\exercice-react\\src\\components'”

My language.js file is definitely sitting in my components folder.我的 language.js 文件肯定位于我的 components 文件夹中。 I do not understand why I get this error?我不明白为什么我会收到这个错误?

portfolio.js

import Language from './language';

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

相关问题 你如何在 React 中创建组件对象? - How do you create component objects in React? 如何在酶测试中测试React组件中的嵌套方法? - How do you test nested methods in a React component in an Enzyme test? 您如何正确地将react导入node-webkit应用程序? - How do you correctly import react into a node-webkit app? 如何将命令 mysqlx 导入我的 React 应用程序? - How do you import the command mysqlx into my React App? 将 bootstrap 5 组件导入 create-react-app - Import bootstrap 5 component into create-react-app 如何正确使用 Typescript 中的 React.lazy() 来导入带有泛型类型参数的反应组件? - How do you correctly use React.lazy() in Typescript to import a react component with a generic type parameter? 如何在反应中使用登录组件创建多页面应用程序? - How do I create a multiple page app with a login component in react? 你如何在 create-react-app 中使用 sass 和 typescript? - how do you use sass in create-react-app with typescript? 有没有办法导入scss文件以使用react-create-app对组件进行反应? - Is there a way to import scss file to react component with react-create-app? 如何从不同的 create-react-app 项目本地导入 React 组件? - How to import React component locally from a different create-react-app project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM