vue实现复制粘贴的两种形式
方式一:
1.安装clipboard:npm install clipboard
2.src/utils/clipboard.js
import Vue from 'vue'
import Clipboard from 'clipboard' function clipboardSuccess() {
console.log('success')
Vue.prototype.$message({
message: 'Copy successfully',
type: 'success',
duration: 1500
})
} function clipboardError() {
console.log('error')
Vue.prototype.$message({
message: 'Copy failed',
type: 'error'
})
} export default function handleClipboard(text, event) {
const clipboard = new Clipboard(event.target, {
text: () => text
})
clipboard.on('success', () => {
clipboardSuccess()
clipboard.off('error')
clipboard.off('success')
clipboard.destroy()
})
clipboard.on('error', () => {
clipboardError()
clipboard.off('error')
clipboard.off('success')
clipboard.destroy()
})
clipboard.onClick(event)
}
3.vue代码
<template>
<div class="app-container">
<el-tabs>
<el-tab-pane label="直接使用剪切板">
<div class="el-tab-pane" >
<el-input v-model="inputData" style='width:400px;'></el-input>
<el-button @click="handleCopy(inputData,$event)">复制</el-button>
</div>
</el-tab-pane>
</el-tabs>
</div>
</template>
<script>
import clip from '@/utils/clipboard'
export default {
name: "index",
data(){
return {
inputData:""
}
},
methods:{
handleCopy(text, event) {
clip(text, event)
console.log('clicp')
}
}
}
</script> <style scoped> </style> <el-tab-pane label="使用封装的剪切指令v-directive">
<div class="el-tab-pane" >
<el-input style='width:400px;'></el-input>
<el-button>复制</el-button>
</div>
</el-tab-pane>
方式二:src/directive/clipboard/clipboard.js
src/directive/clipboard/index.js
npm install clipboard --save
//index.js
import Clipboard from './clipbloard' const install = function(Vue) {
Vue.directive('Clipboard', Clipboard)
} if (window.Vue) {
window.clipboard = Clipboard
Vue.use(install); // eslint-disable-line
} Clipboard.install = install
export default Clipboard
//clipboard.js
// Inspired by https://github.com/Inndy/vue-clipboard2
const Clipboard = require('clipboard')
if (!Clipboard) {
throw new Error('you shold npm install `clipboard` --save at first ')
} export default {
bind(el, binding) {
if (binding.arg === 'success') {
el._v_clipboard_success = binding.value
} else if (binding.arg === 'error') {
el._v_clipboard_error = binding.value
} else {
const clipboard = new Clipboard(el, {
text() { return binding.value },
action() { return binding.arg === 'cut' ? 'cut' : 'copy' }
})
clipboard.on('success', e => {
const callback = el._v_clipboard_success
callback && callback(e) // eslint-disable-line
})
clipboard.on('error', e => {
const callback = el._v_clipboard_error
callback && callback(e) // eslint-disable-line
})
el._v_clipboard = clipboard
}
},
update(el, binding) {
if (binding.arg === 'success') {
el._v_clipboard_success = binding.value
} else if (binding.arg === 'error') {
el._v_clipboard_error = binding.value
} else {
el._v_clipboard.text = function() { return binding.value }
el._v_clipboard.action = function() { return binding.arg === 'cut' ? 'cut' : 'copy' }
}
},
unbind(el, binding) {
if (binding.arg === 'success') {
delete el._v_clipboard_success
} else if (binding.arg === 'error') {
delete el._v_clipboard_error
} else {
el._v_clipboard.destroy()
delete el._v_clipboard
}
}
}
view/clipboard/index.vue
<template>
<div class="app-container">
<el-tabs>
<el-tab-pane label="直接使用剪切板">
<div class="el-tab-pane" >
<el-input v-model="inputData" style='width:400px;'></el-input>
<el-button @click="handleCopy(inputData,$event)">复制</el-button>
</div>
</el-tab-pane>
<el-tab-pane label="使用封装的剪切指令v-directive">
<div class="el-tab-pane" >
<el-input v-model="inputData" placeholder="Please input" style='width:400px;'></el-input>
<el-button type="primary" icon="document" v-clipboard:copy='inputData' v-clipboard:success='clipboardSuccess'>copy</el-button>
</div>
</el-tab-pane>
</el-tabs>
</div>
</template>
<script>
import clipboard from '@/directive/clipboard/index.js' // use clipboard by v-directive export default {
name: "index",
data(){
return {
inputData:""
}
},
directives: {
clipboard
},
methods:{
clipboardSuccess() {
this.$message({
message: 'Copy successfully',
type: 'success',
duration: 1500
})
}
}
}
</script> <style scoped> </style>
vue实现复制粘贴的两种形式的更多相关文章
- C++:一般情况下,设计函数的形参只需要两种形式
C++:一般情况下,设计函数的形参只需要两种形式.一,是引用形参,例如 void function (int &p_para):二,是常量引用形参,例如 void function(const ...
- vue中使用echarts的两种方法
在vue中使用echarts有两种方法一.第一种方法1.通过npm获取echarts npm install echarts --save 2.在vue项目中引入echarts 在 main.js 中 ...
- jquery插件的两种形式
这里总结一下jquery插件的两种形式,一种是通过字面量的形式组织代码,另一种是通过构造函数的方式.下面就两种形式来分析俩个例子. 例子1: ;(function ($,window,document ...
- SQL 关于apply的两种形式cross apply 和 outer apply(转)
转载链接:http://www.cnblogs.com/shuangnet/archive/2013/04/02/2995798.html apply有两种形式: cross apply 和 oute ...
- SQL 关于apply的两种形式cross apply 和 outer apply
SQL 关于apply的两种形式cross apply 和 outer apply 例子: CREATE TABLE [dbo].[Customers]( ) COLLATE Chinese_PRC_ ...
- SQL关于apply的两种形式cross apply和outer apply(转载)
SQL 关于apply的两种形式cross apply 和 outer apply apply有两种形式: cross apply 和 outer apply 先看看语法: <lef ...
- 在 Perl看来, 字符串只有两种形式. 一种是octets, 即8位序列, 也就是我们通常说的字节数组. 另一种utf8编码的字符串, perl管它叫string. 也就是说: Perl只熟悉两种编
在 Perl看来, 字符串只有两种形式. 一种是octets, 即8位序列, 也就是我们通常说的字节数组. 另一种utf8编码的字符串, perl管它叫string. 也就是说: Perl只熟悉两种编 ...
- Controller@实现Controller的两种形式
实现Controller的两种形式 形式1:仅仅实现IController接口,自定义Controller对Request的实现.形式2:在实现IController接口以后,继承Controller ...
- 在sql中case子句的两种形式
case子句,在select后面可以进行逻辑判断. 两种形式:判断相等.判断不等 一.判断相等的语法: case 列名 when ... then ... when ... then ... el ...
随机推荐
- 传统数据库没落,OLTP新型数据库发展火热
參考资料: (1) <OLTP Through the Looking Glass, and What We Found There> (2) <The End of an Arch ...
- 修复eclipse build-helper-maven-plugin 异常
安装 help --> install new http://repo1.maven.org/maven2/.m2e/connectors/m2eclipse-buildhelper/0.15. ...
- 安装调试Installing Odoo
来自odoo的安装步骤 There are mutliple ways to install Odoo, or not install it at all, depending on the inte ...
- 如何提高SELECT的效率
首先避免使用in ,not in,<>,<,<=,>,>=,is null,is not null 主要搜索字段建立索引 .WHERE子句中的连接顺序 sql解 ...
- Linux安装SQLite轻量级数据库
SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中.它是D.RichardHipp建立的公有领域项目.它的设计目标是嵌入式的,而且目前已经在很多嵌入式产 ...
- Java中初级数值类型的大小, volatile和包装类wrapped type的比较
Java中的初级数值类型 Java是静态类型语言, 所有的变量必须先声明再使用. 其初级类型一共8种: boolean: 数据只包含1bit信息, 但是占空间为8-bit, 默认值为false byt ...
- 【微信小程序】详解wx:if elif else的用法(搭配view、block)
1.搭配view <view wx:if="{{boolean==true}}"> <view class="bg_black">< ...
- servlet 转发和超链接转发
超链接属于客户端跳转,request是无法取得属性的 我们知道一个jsp相当与一个servlet 例如,客户端请求A.jsp页面,在A.jsp页面调用request.getAttribute方法放入属 ...
- Mac 通过活动监视器关闭卡死进程
前言: 心好累,Lantern太不省事了 之前装过之后,就设定了开启自启动,搞得我上网都受影响(这玩意,qq没事,但是网易云之类的一些软件上网都不行了...就是这玩意搞的鬼) 没办法,点击关闭吧... ...
- 给maven仓库添加镜像
用maven下载好慢啊,快试试阿里云镜像吧!修改$MAVEN_HOME/conf/settings.xml <!-- 阿里云仓库 --> <mirror> <id> ...