1.新建文件common.js,如下:
export default{
changeImage:function(){
let imgHeight = window.document.querySelector("#code").offsetHeight; // 獲取DOM高度
},
//將圖片Base64 轉成文件
dataURLtoFile:function(dataurl, filename) {
console.log("轉文件")
var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, {type:mime});
}
}
2.在main.js引入並且掛載
import common from './assets/common.js';
Vue.prototype.common = common
3.在vue文件中調用
this.common.changeImage();