“未捕获的语法错误:无法在模块外使用导入语句”reactjs 中的错误

马里比尔

我正在向我现有的 html 页面添加反应。按照此处的说明在此处输入链接描述然后这工作得很好,直到我导入不同的组件。

我的 html 代码:

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test2</title>
</head>
<body>
   <h1>Hello World</h1>
   <div id="like_button_container"></div>

   <!-- Load React. -->
   <!-- Note: when deploying, replace "development.js" with "production.min.js". -->
   <script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin>. 
   </script>
   <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" 
    crossorigin></script>

  <!-- Load our React component. -->
  <script src="like_button.js"></script>

</body>
</html>

我的 like_button.js 代码:

    'use strict';
import Another from "./another.js"; //problem here, if you comment this line, it works.

const e = React.createElement;

class LikeButton extends React.Component {
     constructor(props) {
     super(props);
     this.state = { liked: false };
   }

  render() {
        if (this.state.liked) {
       return 'You liked this.';
   }

  return e(
     'button',
     { onClick: () => this.setState({ liked: true }) },
     'Like'
     );
   }
 }

const domContainer = document.querySelector('#like_button_container');
ReactDOM.render(e(LikeButton), domContainer);

和我的 another.js 代码:

   'use strict';

  const e = React.createElement;

 class Another extends React.Component {
       constructor(props) {
       super(props);
       this.state = { liked: false };
    }

  render() {
       if (this.state.liked) {
       return 'You liked this (another).';
    }

   return e(
      'button',
      { onClick: () => this.setState({ liked: true }) },
     'Like'
    );
   }
  }

  export default Another;

谢谢您的回答。

编辑:我在 package.json 文件中添加了“type”:“module”。最新情况如下;

  {
   "name": "test2",
   "version": "1.0.0",
   "description": "",
   "main": "another.js",
   "scripts": {
   "test": "echo \"Error: no test specified\" && exit 1"
     },
   "keywords": [],
   "author": "",
   "license": "ISC",
   "type": "module"
  }

但它仍然不起作用,我在控制台中收到相同的错误。

编辑2:解决问题。仅将 ("type": "module") 添加到 package.json 是不够的,我还必须将 (type="module") 添加到 html 文件的脚本标记中。

莱伦

你需要告诉节点要使用的ESmodule功能,如importexport关键字。

为此,将此行添加到您的package.json文件中。

"type": "module"

package.json指定类型的示例文件。

{
    "name": "pup",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "type": "module"
}

您还需要添加type="module"到以index.html.

<script src="like_button.js" type="module"></script>

将停止您收到的错误。

也值得一看commonjs 与 es6 导入

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

依赖项中的“语法错误:无法在模块外使用导入语句”

未捕获的语法错误:无法在模块 Vue.js 单组件文件外使用 import 语句

语法错误:如果没有模块,则无法使用导入语句,TypeError [ERR_UNKNOWN_FILE_EXTENSION]:捕获22

模块为 CommonJS 时 ts-node 中的“SyntaxError:无法在模块外使用导入语句”错误

angularjs中未捕获的语法错误

在Chrome中使用ES-6导入语法时出现未捕获的语法错误

如何修复“未捕获的语法错误:意外的令牌导入”?

ReactJS:未捕获的语法错误:意外的令牌(

未捕获的错误:语法错误,使用 replaceWith 时无法识别的表达式

未捕获的语法错误:“意外的令牌)”| JavaScript 中的 if else 语句

github部署中未捕获的语法错误

快递应用程序中未捕获的语法错误

未捕获的语法错误:Chrome 中的意外标记 {

未捕获的语法错误意外令牌

未捕获的语法错误意外令牌

未捕获的语法错误:“意外的标记 {”

未捕获的语法错误;意外的令牌 <

未捕获的语法错误:意外的标记“!”

mongorestore未捕获的异常:语法错误

未捕获的语法错误:意外的标记“<”

未捕获的错误:语法错误,jQuery 1.12.4中来自1.7.1js的表达式无法识别

未捕获的错误:语法错误,无法识别的表达式:选择器中的double等于==

jQuery错误-未捕获的错误:语法错误,无法识别的表达式

Wordpress 致命错误:未捕获错误:语法错误

无法捕获语法错误python

使用dropcap.js时出现未捕获的语法错误/未捕获的TypeError

python 2.7中if语句的语法错误

PostgreSQL中的EXECUTE语句语法错误

JS中的if语句语法错误