简体   繁体   English

React 找不到模块

[英]Module not found by React

I am making calculator by react .我正在通过react制作计算器。
An error occurred trying to separate code into module.尝试将代码分离到模块中时发生错误。
Please let me know why the error occurred and how to solve it.请让我知道错误发生的原因以及如何解决。

Error:错误:
./src/App.js Module not found: Can't resolve 'Cache' in '$HOME/calculator/src'

App.js应用程序.js

import React, { Fragment, Component } from "react";
import Cache from "Cache";

class App extends Component {
  render() {
return <Calculator />;
  }
}

class Calculator extends Component {
  return (
    <Fragment>
      <Cache />
    </Fragment>
  );
}

export default App;

Cache.js缓存.js

import React, { Fragment, Component } from "react";

class Cache extends Component {
  render() {
    return (
      <Fragment>
        <h2>Cache</h2>
      </Fragment>
    );
  }
}

export default Cache;

This is my Github URL:这是我的 Github 网址:
https://github.com/kaibara/calculator/pull/4/files https://github.com/kaibara/calculator/pull/4/files

Thank you谢谢

When you import a file in react, you need to give relative path of the file you are importing with respect to the current component in which you are importing the file.当您在 react 中导入文件时,您需要提供您正在导入的文件相对于您正在导入文件的当前组件的相对路径

Since your app.js and cache.js are in the same folder, so you need to import it like由于您的 app.js 和 cache.js 在同一个文件夹中,因此您需要像这样导入它

import Cache from './Cache . import Cache from './Cache

尝试从文件结构中import Cache from '../xx(where cache is located)';例如import Cache from '../xx(where cache is located)';

if your app.js and Cache.js are in the same folder then add import Cache from "./Cache";如果你的app.jsCache.js在同一个文件夹中,那么添加import Cache from "./Cache"; before Cache.js .Cache.js之前。

if Cache.js is in another folder then write the relative path of Cache.js into app.js如果 Cache.js 在另一个文件夹中, Cache.js的相对路径写入app.js

The issue is with import Cache from "Cache";问题在于import Cache from "Cache";

You are trying to load installed npm module called Cache.您正在尝试加载名为 Cache 的已安装 npm 模块。 Since you are importing a module from file, you need to use the following:由于您是从文件导入模块,因此您需要使用以下内容:

import Cache from "./Cache";

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

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