- 安装
npm install --save clipboard
- tool.js
import Clipboard from "clipboard";
export function toCopy(name) {
var clipboard = new Clipboard(name);
clipboard.on("success", () => {
window.newVue.$toast("复制成功!")
clipboard.destroy();
});
clipboard.on("error", () => {
clipboard.destroy();
});
}
- main.js
import { toCopy } from "./server/tool"
Vue.prototype.$toCopy = toCopy
- 文件中使用
<template>
<div
class="copyAddress"
@click="copyAddress"
:data-clipboard-text="123"
>复制按钮</div>
<!-- data-clipboard-text绑定的是要复制的内容123 -->
</template>
<script>
export default {
methods:{
copyAddress() {
this.$toCopy(".copyAddress");
},
}
}
</script>