vue封装公共方法并调用
1.新建文件common.js,如下:export default{changeImage:function(){let imgHeight = window.document.querySelector("#code").offsetHeight; // 获取DOM高度},//将图片Base64 转成文件dataURL...
·
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();
更多推荐
已为社区贡献27条内容
所有评论(0)