javascript - 将 Canvas 应用到我的 Vue.js 项目时出错
问题描述
我想获取我的 vue.js 项目的屏幕截图。因此我将它用于 html2canvas。我使用以下步骤来应用 html2canvas。
步骤 1-:将“html2canvas”安装到我的项目中
npm install html2canvas
第 2 步:在我的项目代码中导入 html2 画布。
项目代码-:
<template>
<div class="hello">
<div id="photo">
<p>This is vue screenshor</p>
</div>
<button v-on:click="screenshot">Take Picture</button>
</div>
</template>
<script>
//import html2canvas from 'vue-html2canvas';
import html2canvas from 'html2canvas';
export default {
name: 'HelloWorld',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
},
methods:{
screenshot(){
console.log('Function Screenshot');
html2canvas(document.getElementById('photo')).then(canvas => {
document.body.appendChild(canvas)
});
}
}
}
</script>
但我正在运行这个项目,它给出了以下错误-:
ReferenceError: html2canvas is not defined
我如何解决这个问题?
解决方案
我解决了这个错误。从'html2canvas'中删除导入语句并将我的功能更改为如下
screenshot(){
const html2canvas = require('html2canvas');
console.log('Function Screenshot');
html2canvas(document.getElementById('photo')).then(function(canvas){
document.body.appendChild(canvas)
});
}
推荐阅读
- html - HTML 字段占据页面的整个宽度?
- aws-lambda - 在使用无服务器的部署阶段(CodePipeline)将秘密传递给 lambda?
- git - 如何在本地申请 Pull Request?
- docker - 如何设置 docker 以使用另一个位置的现有磁盘映像?
- node.js - 有没有一种简单的方法可以从 express-gateway 服务生成 API 文档?
- sql-server - 如何将参数从 SQL Server 代理作业传递到存储过程
- javascript - 根据关键字过滤数组
- unity3d - Unity3D Hololens 访问 Windows.Storage.KnownFolders
- stored-procedures - 基于一个查询从游标返回 2 个结果集(嵌套游标)
- python-3.x - 模拟运行时如何在 SUMO 中生成车辆数量?