import - 模块不断刷新整个应用程序
问题描述
我正在尝试使用 vanilla Javascript 模块创建一个列表应用程序,但是当我尝试从另一个模块操作另一个模块中的代码时,即使我正在使用热模块替换 (HMR),整个应用程序也会不断重新加载。
index.js:
const actionsContainer = document.querySelector(".actions-container");
const createBtn = document.querySelector(".button");
const notes = document.querySelector("#notes");
if (createBtn) {
createBtn.addEventListener("click", function () {
window.location = "edit.html";
});
}
export function createEl(value) {
const html = `<a href="./edit.html#24d36cc5-1387-4fac-aa08-895718ce00b4" class="list-item">
<p class="list-item__title">${value}</p>
<p class="list-item__subtitle">Last edited 2 days ago</p>
</a>`;
notes.insertAdjacentHTML("afterbegin", html);
}
if (module.hot) {
module.hot.accept();
}
编辑.js:
import { createEl } from "./index.js";
const noteTitle = document.querySelector("#note-title");
const noteBody = document.querySelector("#note-body");
const submitBtn = document.querySelector("#submit");
submitBtn.addEventListener("click", function (e) {
e.preventDefault();
if (noteTitle.value.length == 0 || noteBody.value.length == 0) {
return;
} else {
createEl(noteTitle.value);
// window.location = "index.html";
}
});
if (module.hot) {
module.hot.accept();
}
解决方案
推荐阅读
- django - 浏览器在重新加载 Angular 网页后进行 HTTP 调用
- mysql-error-1064 - 你会帮我吗?我不断收到错误代码 1064,我不明白为什么
- json - 我只需要从 api 获取文本标题和文本描述
- office365 - 无法在 C# 错误中在 Office 365 中发送电子邮件
- sybase - 需要使用查询从“aaaa-h0321456-f7894-1599-5”获取第一个破折号和第二个破折号之间的值
- php - CSS 不适用于 WordPress 下 WooCommerce 中的媒体查询
- angular - 在 Angular 中的同一组件之间制作动画
- javascript - 垂直菜单滚动 CSS
- reactjs - 你如何从另一个反应类组件调用方法
- omnet++ - 使用 Mininet 和 ODL 实现带内控制平面