我是React的初学者,并且在编译器错误中挣扎。让我介绍我的情况。我有两个独立的React应用程序:
- 应用程序A -Big ERP
- 应用B-“插件”在App A中,
我认为我会开发应用B作为独立应用程序。然后,我将其安装到应用程序A(使用 npm install /... )一旦我完成了应用B的开发。我希望我将在应用程序中调用App B中的组件为源代码。一切都很好,直到我运行汇编为止。我正在收到:
SyntaxError: /frontend/node_modules/connector_frontend/src/views/Connector/FormView/index.js: Unexpected token
在我的/frontend/node_modules/connector_frontend/src/views/connector/formview/index.js
中有以下代码:
const ConnectorFormView = ({ AppValues, secureFetch, ...rest }) => {
return (
<p>Hello world</p>
)
}
export default ConnectorFormView;
错误是在>&lt; p&gt; p&gt;
。
我从应用程序调用此功能组件( frontend/src/views/connector/connectorneweditview/index.js
),
import ConnectorFormView from "connector_frontend/src/views/Connector/FormView";
const ConnectorNewEditView = () => {
return (<ConnectorFormView AppValues={appValues} secureFetch={secureFetch} />)
}
export default ConnectorNewEditView;
我试图只返回 connectorforforfiew
的纯文本。像这样的组件:
const ConnectorFormView = ({ AppValues, secureFetch, ...rest }) => {
return (
'Hello world'
)
}
export default ConnectorFormView;
它是成功编译的,但是一旦我从 connectorformview
组件返回JSX后,编译器就会崩溃。
谁能解释此错误的根源?
I am beginner in React and I am struggling with compiler error. Let me introduce my situation. I have two independent React applications:
- App A - Big ERP
- App B - "Plugin" to the App A
I supposed I will develop App B as an independent application. Then, I will install it to the App A (using npm install [email protected]/...
) once I finish development of the App B. I expected I will call components from App B within the App A source code. Everything went fine until I run the compilation. I am receiving:
SyntaxError: /frontend/node_modules/connector_frontend/src/views/Connector/FormView/index.js: Unexpected token
In my /frontend/node_modules/connector_frontend/src/views/Connector/FormView/index.js
there is following code:
const ConnectorFormView = ({ AppValues, secureFetch, ...rest }) => {
return (
<p>Hello world</p>
)
}
export default ConnectorFormView;
Error is ocuring at the position of <p>
.
I call this functional component from App A (frontend/src/views/Connector/ConnectorNewEditView/index.js
) like this
import ConnectorFormView from "connector_frontend/src/views/Connector/FormView";
const ConnectorNewEditView = () => {
return (<ConnectorFormView AppValues={appValues} secureFetch={secureFetch} />)
}
export default ConnectorNewEditView;
I tried to return just a plain text from the ConnectorFormView
component like this:
const ConnectorFormView = ({ AppValues, secureFetch, ...rest }) => {
return (
'Hello world'
)
}
export default ConnectorFormView;
and it was compiled successfully, but once I return a JSX from the ConnectorFormView
component the compiler get crashed.
Can anyone explain the source of this error please?
发布评论
评论(1)
我成功地找出了这个问题的根源,也找到了一个解决方案。我希望我可以重复使用JSX中实现的反应组件。我希望,唯一的要求是安装它,resp。使用NPM以JSX表单下载到我的项目。这是错误的。
根据我的研究,我只能将React组件从JSX汇编为ES。使用Babel可以实现。另一个非常重要的要求,它不应在我安装时作为React应用程序安装。必须将其安装为具有明确定义的导出组件集的NPM软件包。
我发现本文我逐步遵循它,最后我取得了预期的结果。
我可以非常简短地提及我执行的步骤:
/src/index.js
文件中,以将其导出到安装的主机应用程序/babel.config.json
,以下内容package.json
以将React组件编译为ES:执行
NPM运行构建
以将React组件编译为ES。编译的组件位于新dist
文件夹中。调整后的
package.json
现在看起来像这样按 dist git的文件夹。
使用NPM
安装到主机项目的存储库
实际上就是全部。我非常建议您在此答案的开头查看附带的纸张,因为我可能会为您跳过非常重要的信息。
I successfully figured out the source of this problem and also I found a solution. I expected I can reuse however React component implemented in JSX. I expected, the only requirement is to install it, resp. download it in JSX form to my project using NPM. It's FALSE.
According to my research, I can reuse React components compiled from JSX to ES only. It is achievable using Babel. Another very important requiremen, it should not by installed as React application as I installed it. It must be installed as a NPM package with clearly defined set of exported components.
I found this paper https://levelup.gitconnected.com/publish-react-components-as-an-npm-package-7a671a2fb7f and I followed it step by step and finally I achieved desired result.
I can very briefly mention steps which I performed:
/src/index.js
file in order to export it for host App/babel.config.json
with following contentpackage.json
to compile React components to ES:Executed
npm run build
to compile React components to ES. Compiled components are located in newdist
folder.Adjusted
package.json
that looks like this nowPushed
dist
folder to the git.Installed repository to the host project using NPM
Actually that's all. I very recommend to see the attached paper at the beginning of this answer because I maybe skipped very important info for you.