/**
*
* @param {*} url // 图片路径
* @param {*} name // 图片名称
*/
Vue.prototype.imagesDownload = function (url, name) {
let image = new Image()
image.setAttribute("crossOrigin", "anonymous");
image.src = url;
image.onload = () => {
let canvas = document.createElement("canvas");
canvas.width = image.width;
canvas.height = image.height;
let ctx = canvas.getContext("2d");
ctx.drawImage(image, 0, 0, image.width, image.height);
canvas.toBlob((blob) => {
let href = URL.createObjectURL(blob);
let Link = document.createElement("a");
Link.download = name;
Link.href = href;
Link.click();
Link.remove();
// 用完释放URL对象
URL.revokeObjectURL(href);
});
};
}
博主的是全局方法挂载,用的时候根据自己需要更改