Uncaught TypeError: (intermediate value)(…) is not a function, putting semicolon like I've read online is not working

Uncaught TypeError: (intermediate value)(…) is not a function, putting semicolon like I've read online is not working

我正在为 odin 项目制作一个小模型餐厅页面以开始熟悉 webpack,当我 运行 遇到这个烦人的错误时:

Uncaught TypeError: ((intermediate value)(intermediate value)(...) , (void (intermediate value)(intermediate value)(intermediate value)(intermediate value)(intermediate value)(intermediate value)(intermediate value)(intermediate value)(...))) is not a function

这是我的代码,它说它发生在第 36 行(在 makePage 函数中说“navBar()”的行):

function makeBackground() {
    const background = document.createElement('div');
    background.classList.add("background");
    document.body.appendChild(background);
};
function navBar() {
    const content = document.querySelector('#content');
    const header = document.createElement('header');
    const ul = document.createElement("ul");
    const nav = document.createElement('nav');

    content.append(header);
    header.append(nav);
    nav.append(ul);

    const home = document.createElement('li');
    home.setAttribute("id", 'home');
    home.textContent = "Home";
    ul.append(home);

    const menu = document.createElement("li");
    menu.setAttribute('id', 'menu');
    menu.textContent = "Menu";
    ul.append(menu);

    const contact = document.createElement('li');
    contact.setAttribute('id', 'contact');
    contact.textContent = "Contact";
    ul.append(contact);
};

function makePage() {
    makeBackground();
    navBar();
};

export default makePage();

然后我将该函数导出到我的 index.js,并在那里调用该函数。

您必须在没有 () 的情况下执行 export default makePage;。使用 export default makePage(); 导出未定义的 makePage 函数 returns。所以试试这个:

function makeBackground() {
    const background = document.createElement('div');
    background.classList.add("background");
    document.body.appendChild(background);
}
function navBar() {
    const content = document.querySelector('#content');
    const header = document.createElement('header');
    const ul = document.createElement("ul");
    const nav = document.createElement('nav');

    content.append(header);
    header.append(nav);
    nav.append(ul);

    const home = document.createElement('li');
    home.setAttribute("id", 'home');
    home.textContent = "Home";
    ul.append(home);

    const menu = document.createElement("li");
    menu.setAttribute('id', 'menu');
    menu.textContent = "Menu";
    ul.append(menu);

    const contact = document.createElement('li');
    contact.setAttribute('id', 'contact');
    contact.textContent = "Contact";
    ul.append(contact);
}

function makePage() {
    makeBackground();
    navBar();
}

export default makePage;