【问题标题】:Why I couldn't inject default index.js file to the root html?为什么我不能将默认 index.js 文件注入到根 html 中?
【发布时间】:2020-12-09 14:54:56
【问题描述】:

我是反应 js 的新手。在我设置项目文件夹开始编码后,我删除了 src 文件夹并重新创建了它。在 src 中,我创建了 index.js 并导入了所有的 react 库。但是我没有在 localhost:3000 页面上看到我的 hello world。可能是什么原因?

【问题讨论】:

  • 您的index.html 代码是什么样的?
  • 要获得其他用户的帮助,您必须分享您添加的代码(删除 src 文件夹后所做的更改)
  • 我没有更改 index.html 代码。但我的 index.js 代码如下 `import React from 'react';从 'react-dom' 导入 ReactDOM; ReactDOM.render(
    hello React
    ,document.getElementById('root'));`
  • 你看到你的 bundle js 文件被注入到你的 html 文件了吗?
  • 听起来像是指定index.jsApp.js 的相对路径的问题。

标签: reactjs


【解决方案1】:
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
class Index extends Component {
render(){
return(
<div><h1>hello world</h1></div>
)
}
}
ReactDOM.render(<Index />, document.getElementById('root'));

【讨论】:

  • 您能解释一下这是什么或它如何解决 OP 遇到的问题吗?
【解决方案2】:
  1. 删除您的项目文件夹
  2. 然后你重新启动npx create-react-app appname,因为你还没有编写任何逻辑,你可能已经删除了一个需要的依赖文件

清理 hello world 时,不要删除整个 src 文件夹。只需删除src文件夹中除app.js和index.js外的文件

【讨论】:

    猜你喜欢
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2010-12-17
    • 2013-08-30
    • 1970-01-01
    相关资源
    最近更新 更多