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

ih99xse1  于 2022-12-15  发布在  其他
关注(0)|答案(2)|浏览(229)

我尝试将变量导出到另一个模块,但发生错误。未捕获的语法错误:意外的标记“export”。
文件结构:

./css:
select.css  style.css

./html:
index.html  make.html  select.html

./javascript:
make.js  script.js  select.js

index.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>Word Search Maker</title>
</head>
<body>
    <div class="title-h1">
        <h1>
            Simple Word Search maker
        </h1>
    </div>
    <div class="start">
        <button id="start-button">Start ➝</button>    
    </div>
    <script src="/javascript/script.js"></script>
    <link rel="stylesheet" href="/css/style.css">
</body>
</html>

script.js

const startButton = document.querySelector("#start-button");
const activeclass = "active";

function handlestartBtnMouseEnter() {
    startButton.classList.add(activeclass);
}

function handlestartBtnMouseLeave() {
    startButton.classList.remove(activeclass);
}

function handleStartBtnClick() {
    window.location.href = "/html/select.html";
}

startButton.addEventListener("mouseenter", handlestartBtnMouseEnter);
startButton.addEventListener("mouseleave", handlestartBtnMouseLeave);
startButton.addEventListener("click", handleStartBtnClick);

select.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>Making Basic template</title>
</head>
<body>
    <h1>
        Basic template:
    </h1>
    <div class="settings">
        width: <input type="text" id="width">
        <br>
        height: <input type="text" id="height">
    </div>
    <button id= "make-button">Make</button>
    <script type="module" src="/javascript/select.js"></script>
</body>
</html>

select.js:

let width_textbox = document.getElementById("width");
let height_textbox = document.getElementById("height");
let width = null;
let height = null;

const makebutton = document.querySelector("#make-button");

function handleClickMakeBtn() {
    width = parseInt(width_textbox.value);
    height = parseInt(height_textbox.value);
    if (width > 30 || height > 30) {
        alert("Width and height cannot longer than 30!");
        width_textbox.value = "";
        height_textbox.value = "";
        } else if (width < 5 || height < 5) {
        alert("Width and height cannot shorter than 5!");
        width_textbox.value = "";
        height_textbox.value = "";
    } else if (isNaN(width) || isNaN(height)) {
        alert("Width and height must be number!");
        width_textbox.value = "";
        height_textbox.value = "";
    } else if (width == null || height == null) {
        alert("You have to enter width and height!");
    } 
    else {
        window.location.href = "/html/make.html";
    }
    export { width, height }
}


makebutton.addEventListener("click", handleClickMakeBtn);

make.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>Make word search</title>
</head>
<body>
    <script type="module" src="/javascript/make.js"></script>
</body>
</html>

make.js:

import * as settings from "./select.js";

for (i=0; i <= width; i ++) {
    document.createElement('input') * settings.width;
    console.log(settings.height);
}

我对网络很陌生。如果是我的错,对不起。

c6ubokkw

c6ubokkw1#

问题来自函数handleClickMakeBtn
你不能像那样导出函数中的对象
如果要返回值,则必须使用关键字return
但是像宽度和高度一样是全局变量当你在函数中修改它的时候它就是修改全局变量

a64a0gku

a64a0gku2#

你只能导出文件顶层的东西(即不在函数内部)。
但是,您可以使用窗口对象从任何地方访问变量。
像这样

function handleClickMakeBtn() {
    width = parseInt(width_textbox.value);
    height = parseInt(height_textbox.value);
    window.settings = { width, height }
}

现在您可以从任何地方访问该对象。
我不推荐这种方式,相反,你可以在模块内部创建一个全局对象,然后在顶层导出它。

相关问题