【问题标题】:Expected an identifier and instead saw '<'. React.js Project需要一个标识符,但看到的是“<”。 React.js 项目
【发布时间】:2020-09-02 23:35:10
【问题描述】:

我正在创建一个简单的电子商务 React 项目来学习,我正在使用 Visual Studio。但是,我收到了这些错误:

Errors

我的 index.js 代码是

code

谢谢

【问题讨论】:

  • 最好将代码作为代码而不是图像放在此处。

标签: css reactjs


【解决方案1】:

我不确定您的文件结构如何,但是当您初始化 React 项目时,“App.js”和“index.js”应该在您的根目录中。然后您将 App 导入为import App from './App';

【讨论】:

    【解决方案2】:

    参考这个问题:Expected an identifier and instead saw '>'

    "JSHint 不支持 jsx 的 linting。如果你想使用 jsx 开发 react 应用程序,你应该禁用它或更好地切换到 ESLint。

    对于 Visual Studio Code,您可以安装一个插件。"

    在您的设置 > 用户设置中,如果还没有,请添加它。

    "jshint.options": {
          "esversion":6
      }
    

    【讨论】:

      【解决方案3】:

      如果 JSX 没有被正确编译,就会出现这个错误。见例子:

      <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script>
      
      <div id="root"></div>
      
      <script>
        function App() {
          return (
            <div>Sample Component</div>
          );
        }
      
        ReactDOM.render(<App/>, document.getElementById("root"))
      </script>

      要解决这个问题,使用Babel等技术来启用JSX语法

      <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script>
      
      <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
      
      <div id="root"></div>
      
      <script type="text/babel">
        function App() {
          return (
            <div>Sample Component</div>
          );
        }
      
        ReactDOM.render(<App/>, document.getElementById("root"))
      </script>

      【讨论】:

        猜你喜欢
        • 2019-05-07
        • 2018-10-18
        • 1970-01-01
        • 1970-01-01
        • 2018-09-07
        • 1970-01-01
        • 1970-01-01
        • 2014-11-27
        • 2023-03-11
        相关资源
        最近更新 更多