简体   繁体   English

有没有办法在没有 create-react-app 的情况下在 React 中导入模块?

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

I've just started using react and was using the create-react-app from npm until today.我刚刚开始使用 react 并且一直使用 npm 中的 create-react-app 直到今天。 I've gotten the react and reactdom code from我已经从

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

I've saved the files as:我已将文件保存为:

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

This is my index.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>

I have created a module called Greetings, given below:我创建了一个名为 Greetings 的模块,如下所示:

//Test/Components/Greetings.js

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

export default Greetings;

And I've created an App.js, given here:我创建了一个 App.js,如下所示:

//Test/App.js

import Greetings from "./Components/Greetings";

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

I'm getting this error:我收到此错误:

Uncaught SyntaxError: Unexpected identifier

Is there an easy way to import a react module?有没有一种简单的方法来导入反应模块?

import and export are Node.js keywords. importexport是 Node.js 关键字。 On the client-side you don't need them.在客户端,您不需要它们。 Simply include your javascript files in the html:只需在 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