簡體   English   中英

有沒有辦法在沒有 create-react-app 的情況下在 React 中導入模塊?

[英]Is there a way to import modules in React without create-react-app?

我剛剛開始使用 react 並且一直使用 npm 中的 create-react-app 直到今天。 我已經從

https://unpkg.com/react@15/dist/react.js
https://unpkg.com/react-dom@15/dist/react-dom.js

我已將文件保存為:

/Test/react/react.js
/Test/react/react-dom.js

這是我的 index.js:

<!--/Test/index.js-->

<html>

<head>
    <title>React Hello World</title>
</head>

<body>
    <div id="root"></div>

    <script src="react/react.js"></script>
    <script src="react/react-dom.js"></script>
    <script src="App.js"></script>
</body>

</html>

我創建了一個名為 Greetings 的模塊,如下所示:

//Test/Components/Greetings.js

class Greetings extends React.Component {
  render() {
    return React.createElement(
      "h1",
      null,
      "Greetings, " + this.props.name + "!"
    );
  }
}

export default Greetings;

我創建了一個 App.js,如下所示:

//Test/App.js

import Greetings from "./Components/Greetings";

ReactDOM.render(
  React.createElement(Greetings, { name: "Me" }),
  document.getElementById("root")
);

我收到此錯誤:

Uncaught SyntaxError: Unexpected identifier

有沒有一種簡單的方法來導入反應模塊?

importexport是 Node.js 關鍵字。 在客戶端,您不需要它們。 只需在 html 中包含您的 javascript 文件:

<script src="App.js"></script>
<script src="Greetings.js"></script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM