基于vue3的uniapp开发微信小程序在canvas保存的时候报错。uniapp +vue3+options语法中,遇到了这个问题,最后的解决办法是直接在script里面定义个变量,用来存取canvas对象。
如果定义在data中的变量赋值以后,比如我把 res[0].node复制给 this.canvas2. 用的时候发现对象类型是GQ,正常应该是Object,读取不到width。就报错了。
canvasToTempFilePath:fail Cannot read property 'width' of undefined
<script>
//不使用代理变量
var currentCanvas = null;
export default {
// 获取canvas实例
const queryCanvas = uni.createSelectorQuery();
queryCanvas
.select('#canvas')
.fields({
node: true,
size: true
})
.exec((res) => {
currentCanvas = res[0].node;
// 截图
uni.canvasToTempFilePath({
width: cav.width,
height: cav.height,
canvas: currentCanvas, //2d用这个
fileType: "png",
quality: 1,
success(res) {
转载请注明:天狐博客 » Uniapp开发小程序canvasToTempFilePath:fail Cannot read property 'width' of undefined

