vuejs如何实现复制粘贴功能,下面编程教程网小编给大家详细介绍一下实现代码!
新建copy.js的文件
const vCopy = {
bind(el, { value }) {
el.$value = value;
el.handler = () => {
if (!el.$value) {
console.log('无复制内容');
return;
}
const textarea = document.createElement('textarea');
textarea.readOnly = 'readonly';
textarea.style.position = 'absolute';
textarea.style.left = '-9999px';
textarea.value = el.$value;
document.body.appendChild(textarea);
// 选中值并复制
textarea.select();
textarea.setSelectionRange(0, textarea.value.length);
const result = document.execCommand('Copy');
if (result) {
console.log('复制成功');
}
document.body.removeChild(textarea);
};
el.addEventListener('click', el.handler);
},
componentUpdated(el, { value }) {
el.$value = value;
},
unbind(el) {
el.removeEventListener('click', el.handler);
},
};
export default vCopy;
新建directives.js文件
import copy from './copy.js';
// 自定义指令
const directives = {
copy,
};
export default {
install(Vue) {
Object.keys(directives).forEach((key) => {
Vue.directive(key, directives[key]);
});
},
};
main.js全局引入
import Vue from 'vue';
import Directives from './directives';
Vue.use(Directives);
页面调用
<template>
<div >
<button v-copy="copyText">拷贝</button>
</div>
</template>
<script>
export default {
data(){
return {
copyText:'要copy的内容'
}
},
methods: {
init () {
},
},
mounted () {
_this = this;
_this.init();
},
}
</script>
以上是编程学习网小编为您介绍的“vuejs如何实现复制粘贴功能”的全面内容,想了解更多关于 vuejs 内容,请继续关注编程基础学习网。
织梦狗教程
本文标题为:vuejs如何实现复制粘贴功能


基础教程推荐
猜你喜欢
- 鼠标事件的screenY,pageY,clientY,layerY,offsetY属性详解 2023-12-01
- 微信小程序如何调用json数据接口并解析 2024-01-07
- JavaScript 模拟用户单击事件 2024-01-07
- JS+CSS实现滑动切换tab菜单效果 2023-12-28
- JavaScript创建一个欢迎cookie弹出窗实现代码 2024-01-30
- Babylon使用麦克风并处理常见问题解决 2024-01-08
- 「HTML+CSS」--自定义加载动画【025】 2023-10-26
- jquery如何改变html标签的样式(两种实现方法) 2023-12-28
- 解决 Django 渲染模板 与 Vue {{ }} 冲突 2023-10-08
- 详解一级导航的制作 2023-12-28