有没有一种简单的方法可以在脚本标记中包含JSX文件? [英] Is there a simple way to include a JSX file with script tag?

查看:74
本文介绍了有没有一种简单的方法可以在脚本标记中包含JSX文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将myfile.jsx文件中包含的一堆React元素包括在内 <script type="text/babel" src="http://example.com/js/r/myfile.jsx"></script> 这不起作用.
如果我尝试放import "http://example.com/js/r/myfile.jsx";,它会告诉我require未定义.
有没有一种简单的方法,可以通过script标签包含jsx代码?
我仅在网络上使用它,而是使用Babel进行翻译. 下面是我的标头,当所有代码都在同一页面中时,代码可以正常工作.

I am trying to include a bunch of React elements I have in file myfile.jsx <script type="text/babel" src="http://example.com/js/r/myfile.jsx"></script> This does not work.
If I try to put import "http://example.com/js/r/myfile.jsx"; it tells me require is not defined.
Is there a simple way, via script tag to include jsx code?
I am using it on the web only, I use Babel to transpile it. Below is my header, code works fine when all is in the same page.

<script src="https://unpkg.com/react@15/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@15/dist/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>

推荐答案

是的,您必须在脚本标签中添加type='text/jsx'. 例如,

Yes, you have to add type='text/jsx' in the script tag. For example,

<script src="app.jsx" type="text/jsx"></script>

这篇关于有没有一种简单的方法可以在脚本标记中包含JSX文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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