ReactJS inside .NET framework project - Cannot use import statement outside a module

左心房为你撑大大i 提交于 2020-04-14 06:01:08

问题


I've tried to use reactJS inside .net framework MVC application.

View:

<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://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.js"></script>

<div id="root"></div>
@Scripts.Render("~/Scripts/ReactJS/test.jsx")

test.jsx:

import React from 'react';

import ReactDOM from 'react-dom';

const App = () => <div>Hello world!</div>;
ReactDOM.render(<App />, document.getElementById("root"));

Message is not shown and the error is:

test.jsx:2 Uncaught SyntaxError: Cannot use import statement outside a module

I don't understand why is this not working. I have included both react libraries and babel library.


回答1:


In your View, instead of using @Scripts.Render("~/Scripts/ReactJS/test.jsx")

you can try like this <script src="@Url.Content("~/Scripts/ReactJS/test.jsx")"></script> to create the referenced JavaScript file (test.jsx).



来源:https://stackoverflow.com/questions/58907594/reactjs-inside-net-framework-project-cannot-use-import-statement-outside-a-mo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!