首页 > 解决方案 > 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>

标签: javascriptwebpack

解决方案


the reason for duplication was domContentLoaded in addition to invoking main, i was hitting the page twice.


推荐阅读