javascript - webpack - 无法读取 null 的属性“appendChild”
问题描述
当我渲染我的网格时,我收到以下错误:Uncaught TypeError: Cannot read property 'appendChild' of null。我已将脚本标签移至头部,以避免重复网格。我尝试使用 defer,但它带来了重复。有什么想法吗?
function drawMap() {
// debugger
map = document.createElement('div');
let tiles = createTiles(gameData);
tiles.forEach(tile => {
map.appendChild(tile);
});
document.body.appendChild(map);
}
Uncaught TypeError: Cannot read property 'appendChild' of null
at drawMap (index.js:72)
at main (index.js:133)
at Module../src/index.js (index.js:137)
at __webpack_require__ (bootstrap:19)
at Object.0 (main.js:10681)
at __webpack_require__ (bootstrap:19)
at bootstrap:83
at bootstrap:83
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./dist/main.css">
<script src="./dist/main.js"></script>
<title>Document</title>
</head>
<body>
</body>
</html>
解决方案
the reason for duplication was domContentLoaded in addition to invoking main, i was hitting the page twice.
推荐阅读
- javascript - 按名称搜索目录中的文件(不知道扩展名)
- javascript - 在 useEffect 中使用 prop 函数返回:prop.function 不是函数
- angular - Angulars detectChanges() 和 markForCheck() 是否与默认更改检测策略相关?
- c++ - 我无法在代码块中运行 c++ 代码,该怎么办?
- react-native - React Native:你必须为 AsyncStorage 做手动链接吗?
- android - Android Studio:“启动时运行”缺少权限
- vhdl - 当数组类型在同一个包中重载时,如何访问隐式“=”函数?
- android - Android:未触发与包相关的意图
- azure - Azure devops 无法连接到 jfrog Artifactory
- python - 如何在 Python 套接字中传递变量?