<template>
<h2>复制或剪切图片后以ctrl + V 粘贴</h2>
<img id="img" src="" alt="">
</template>
<script>
export default {
methods: {
setPasteImg(){
//粘贴事件
document.addEventListener('paste', function(event){
if (event.clipboardData || event.originalEvent) {
var clipboardData = (event.clipboardData || event.originalEvent.clipboardData);
if(clipboardData.items){
var blob;
for (var i = 0; i < clipboardData.items.length; i++) {
if (clipboardData.items[i].type.indexOf("image") !== -1) {
blob = clipboardData.items[i].getAsFile();
}
}
var render = new FileReader();
render.onload = function (evt) {
//输出base64编码
var base64 = evt.target.result;
document.getElementById('img').setAttribute('src',base64);
}
render.readAsDataURL(blob);
}
}
})
}
},
mounted(){
this.setPasteImg()
}
}
</script>
<style>
#img{
width: 800px;
box-shadow: 0 0 5px blue;
}
</style>