【问题标题】:How index.js file is able to communicate with index.html?index.js 文件如何与 index.html 通信?
【发布时间】:2021-04-04 13:24:00
【问题描述】:

在 React 项目中,index.html 和 index.js 文件都在不同的文件夹中,这两个文件之间没有链接。那么我们如何定位 id 为 HTML 文件根目录的 div 在 index.js 文件中使用 document.getElementById("root")。

请用简单的语言回答这个问题,因为我刚刚开始学习这个,我完全是初学者。

【问题讨论】:

标签: javascript html css reactjs web


【解决方案1】:

React 是目前最好的 JS 库之一。它简化了开发过程,而不会显式地混淆实现细节。当您启动脚本时,index.htmlindex.js 在运行时被链接。 React 在底层使用 webpack,其中 webpack 的入口是 index.js,当它运行时(在所有转换之后)它会在 HTML 文件中注入 JavaScript 代码 index.html。当您在 build 文件夹中构建 bundle 时,您可以在 index.html 中看到注入的代码。

【讨论】:

    猜你喜欢
    • 2022-08-18
    • 2017-06-03
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 2023-03-20
    • 2020-12-03
    • 2022-01-04
    • 1970-01-01
    相关资源
    最近更新 更多