错误:html2canvas 不是 HTMLDivElement 的函数。<anonymous>

问题描述 投票:0回答:1

在 Linux 下,在最新的 Chrome 浏览器中使用最新版本的 html2canvas.js。

每当我尝试使用 html2canvas 时,我都会在标题中收到消息:

exportArea.addEventListener('点击', e =>{

html2canvas(document.querySelector('#tablesArea')).then(canvas => {

let canvasUrl = canvas.toDataURL();

console.log(canvasUrl);

}); });

我认为我并没有真正导入 html2canvas,尽管我已经在 js 脚本的开头编写了这个,并且 html2canvas.js 与 projname.html 和 projname.js 位于同一文件夹中:

从'./html2canvas.js'导入*作为html2canvas;

我也在html文件中写了这个:

为了使用 html2canvas.js 文件,我必须禁用 JS 源映射和 CSS 源映射,否则 Chrome 会抛出与映射相关的错误。如果我使用 .min.js 我不会得到与地图相关的错误,但似乎我必须使用 html2canvas 的非 min 最新版本?...无论如何,即使我使用 .min.js 版本模块的我仍然收到“html2canvas 不是函数”错误。

我是一个十足的菜鸟,但我已经研究了这个问题一整天,没有任何结果 - 遗憾的是,谷歌搜索没有为我提供有关这个问题的大量信息。请帮忙,我束手无策,我不明白为什么这不起作用。

javascript html2canvas
1个回答
0
投票

当您在导入时遇到这些问题时,导入映射是导入模块的好方法:

<head>
    <title>Document</title>
    <script type="importmap">
        {
            "imports": {
              "html2canvas": <!-- <-- this can be anything you want --> "https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.js",
            }
        }
    </script>
</head>

在js中:

import * as html2canvas from "html2canvas" // this should be the name you specified in importmap

Importmaps 在某种程度上是一个导入管理器,它使导入模块变得更加容易。
Cdn.js 是一个拥有几乎所有 javascript 模块(如 jquery 或 Chart.js)的网站,如果 Cdn.js 没有您需要的模块,请使用 jsDelivr,这是另一个导入 javascript 模块的好资源。这些链接-基于模块不需要node.js。 有关导入映射的更多信息:link

© www.soinside.com 2019 - 2024. All rights reserved.