vue封裝公共方法並調用

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();

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章