简体   繁体   English

React 中的“未找到模块:无法解析”

[英]"Module not found: Can't resolve" in React

I've got a react project in a folder 'react-test' and inside are all of the json and node modules etc and the src folder installed by running create-react-app.我在“react-test”文件夹中有一个 react 项目,里面是所有 json 和 node 模块等,以及通过运行 create-react-app 安装的 src 文件夹。

Inside the src folder I've got App.js and a components folder with Titles.js inside.在 src 文件夹中,我有App.js和一个包含Titles.js的组件文件夹。

I want to import a class named Titles from Titles.js .我想从Titles.js导入一个名为Titles的类。

I'm using the relative file path ./components/Titles.js but I get the following error when I do:我正在使用相对文件路径./components/Titles.js但这样做时出现以下错误:

./src/App.js
Module not found: Can't resolve './components/Titles.js' in '/Users/username/Documents/ReactApp1/react-test/src'

This is my Titles.js component:这是我的 Titles.js 组件:

import React from "react";
class Titles extends React.Component {
  render() {
    return (
      <div>
        <h1>Weather Finder</h1>
        <p>Find out temperature, conditions, and more...</p>
      </div>
    );
  }
}

export default Titles;

This is my App.js component:这是我的 App.js 组件:

import React, { Component } from "react";
import Titles from "./components/Titles.js";

export class App extends Component {
  render() {
    return (
      <div>
        <Titles />
      </div>
    );
  }
}

export default App;

I've been trying to mess with the file path in the import statement but nothing has worked so far.我一直试图弄乱导入语句中的文件路径,但到目前为止没有任何效果。 Thank you for your time.感谢您的时间。

Try to put in your Titles.js file at the end of your code :尝试在代码末尾添加 Titles.js 文件:

export default (the name of your component)导出默认值(组件的名称)

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

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