如何使用Babel-Standalone在客户端React JSX中执行导入/导出 [英] How to perform import/export in client-side React JSX using Babel-Standalone
问题描述
我正在使用Babel-Standalone在React应用程序中使用JSX,而不使用NPM. Babel显然将"import"语句转换为"require"语句.导入"require.js"和其他依赖项以使这项工作产生更多错误.
I'm using Babel-Standalone to use JSX in a React application without using NPM. Babel apparently translates 'import' statements into 'require' statements; importing 'require.js' and other dependencies to make this work produces more errors.
当然,必须有一种在客户端JSX上下文中执行导入/导出的简单方法.请告知(这里不寻求Node/NPM/Webpack解决方案;寻求适当库的CDN和重写import语句等).
Surely, there must be a simple way to perform an import/export in the context of client-side JSX. Please advise (no Node/NPM/Webpack solutions are sought here; CDN of appropriate library(ies) and rewrite of import statement, etc., are sought).
<!doctype html>
<html lang="en-us">
<head>
<title>React JSX Babel-Standalone Import/Export Problem</title>
<meta charset="utf-8">
</head>
<body>
<div id="root"></div>
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script type="text/babel">
// See MyExport.js text below this SCRIPT
// Goal: to employ <MyExport /> in the return of App.
// import MyExport from "./MyExport";
const App = () => {
return (
<div>Hello</div>
);
};
ReactDOM.render(<App />, document.querySelector("#root"));
</script>
<!-- MyExport.js:
const MyExport = () => {
return (
<div>MyExport</div>
);
};
export default MyExport;
-->
</body>
</html>
推荐答案
有一个解决方案:(1)包含导出内容的JSX脚本必须与其他元素一起包含在SCRIPT
元素中;没有它,不能简单地被另一个脚本引用. (2)JSX脚本和从中导入的JSX脚本都必须具有自定义属性data-plugins="transform-es2015-modules-umd"
和预期的属性type="text/babel"
.运行以下HTML,对问题中提供的内容进行修改,以提供解决方案(您必须在本地创建MyExport.js
才能运行它):
There is a solution: (1) The JSX script containing the export must be included in a SCRIPT
element along with the others; it cannot simply be referenced by another script without. (2) Both that JSX script and the JSX script importing from it must have the custom attribute data-plugins="transform-es2015-modules-umd"
along with the expected attribute type="text/babel"
. Run the following HTML, a modification of what was provided in the question, which provides the solution (you'll have to create MyExport.js
locally to run it):
<!doctype html>
<html lang="en-us">
<head>
<title>React JSX Babel-Standalone Import/Export Problem</title>
<meta charset="utf-8">
</head>
<body>
<div id="root"></div>
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script data-plugins="transform-es2015-modules-umd" type="text/babel" src="./MyExport.js"></script>
<script data-plugins="transform-es2015-modules-umd" type="text/babel">
import MyExport from "./MyExport";
const App = () => {
return (
<MyExport/>
);
};
ReactDOM.render(<App />, document.querySelector("#root"));
</script>
<!-- MyExport.js:
const MyExport = () => {
return (
<div>MyExport element is imported!</div>
);
};
export default MyExport;
-->
</body>
</html>
我希望这对其他人有帮助.
I hope this helps someone else.
这篇关于如何使用Babel-Standalone在客户端React JSX中执行导入/导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!