JS文件中的React组件-用于HTML文件中脚本的ReactDOM [英] React component in JS file -- ReactDOM to be used in script in HTML file

查看:40
本文介绍了JS文件中的React组件-用于HTML文件中脚本的ReactDOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在HTML文件的脚本中导入组件?
要进行以下工作需要做哪些修改?
Babel库是否应该添加到HTML文件中?(已经安装了Webpack和React)

How to import a component in a script in HTML file?
What are the modifications to be done to make the following work?
And should the Babel libraries be added to the HTML file? (Webpack and React are already installed)

在car.js中

class Car extends React.Component {
   render() { return (<p>nice car</p>) }}

在page.html

In page.html

...
<script type="text/babel">
   import Car from "static/car.js"      //<---------????????

   ReactDOM.render(<Car />, document.querySelector("#root"));
</script>

推荐答案

不需要从"static/car.js" 导入Car,它将在JS文件中工作.

do not need to import Car from "static/car.js" it will works in JS file.

需要参考,例如< script type ="text/babel"src ="static/car.js"></script>

<script src="static/car.js" type="text/babel"></script>
<script type="text/babel">
  ReactDOM.render(<Car />, document.querySelector("#root"));
</script>

这篇关于JS文件中的React组件-用于HTML文件中脚本的ReactDOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆