【问题标题】:"Cannot Use import statement outside a module" Error\"不能在模块外使用 import 语句\" 错误
【发布时间】:2022-10-23 18:18:03
【问题描述】:

我不知道如何解决这个错误,是创建一个模块还是什么。由于我正在使用浏览器,因此 Require JS 将不起作用。如何使用基于客户端的代码解决此错误?我需要安装软件包来解决它吗?请帮忙。

顺便说一句,我的 JS 代码:

import floor from 'mathjs';
import random from 'mathjs';
var colorValue = Array("#000000", "#000000", "#000000");
const hexadecimal = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D","E","F");
listening();




function listening() {
    document.getElementById("changeColorOne").addEventListener(click, changeColor1);
    document.getElementById("changeColorTwo").addEventListener(click, changeColor2);
    document.getElementById("changeColorThree").addEventListener(click, changeColor3);
}
function changeColor1() {
    document.querySelector(".square1").style.background = colorValue[0];
    colorValue[0] = colorDecide();
}
function changeColor2() {
    document.querySelector(".square2").style.background = colorValue[1];
    colorValue[1] = colorDecide();
}
function changeColor3(){
    document.querySelector(".square3").style.background = colorValue[2];
    colorValue[2] = colorDecide();
}
function colorDecide() {
    var tempValue = "#";
    for (var i = 1; i <= 6; i++) {
        let x = floor(random() * 16);

        tempValue = tempValue.concat(hexadecimal[x]);
    }
    return tempValue;
}

【问题讨论】:

标签: javascript import module


【解决方案1】:
<script src="script.js" type="module"></script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-02-12
    • 2020-05-09
    • 2020-02-09
    • 2020-09-12
    • 2020-01-27
    • 2020-02-11
    相关资源
    最近更新 更多