javascript - 将外部 jsx 文件添加到 html 页面(反应)
问题描述
我在 js/script.js 中有一些反应代码 我的 HTML 页面的 head 标签看起来像这样:
<head>
<script src="https://unpkg.com/react@15.6.2/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@15.6.2/dist/react-dom.js"></script>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
<script src="js/script.js"type="text/jsx"></script>
</head>
出于某种原因,当我在 chrome 上打开页面时,我的反应元素没有显示,而在 Firefox 中则显示。我不想将页面限制为 Firefox,也不想使用内联脚本。有什么解决办法吗?
解决方案
如果您直接从 Chrome 运行页面(不带 http),您的页面会尝试运行 javascript,而 Chrome 会阻止它并显示类似于以下内容的错误:“跨源请求仅支持协议方案:http、data、chrome、 chrome-extension, https, chrome-extension-resource”</p>
您应该安装一个 Web 服务器并通过 http 请求打开您的页面。命令字符串:npm install http-server -g 然后运行它:http-sever 'path/with/yourpage.html' 你的页面应该可以通过 URL 访问:localhost:8080/yourpage.html
推荐阅读
- java - 使用 GSON 按键访问 JSON 元素
- python-3.x - Pandas - 你如何对多列进行抱怨并获得最低值?
- python - 在 dag 之间使用单独的 environ 和 sys.path
- ruby-on-rails - 有没有办法测试 PostCSS Autoprefixer 是否工作?专门针对 Rails 6
- python - 如何以较短的列表重复的方式将两个不同长度的列表相乘?
- c# - 创建用户控件时,是否应该在视图和代码隐藏之间使用数据绑定?
- android - 如何在第二个活动中获取 String.xlm 文件?
- git - 如何从 Azure Devops 中的特定分支构建?
- google-apps-script - 根据另一个工作表值填充单元格
- python - 如何在我的环境中设置与基本环境不同的 Python 版本?