1.安装
npm install --save vue-clipboard2
2.main.js
import Vue from 'vue'
import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)
- 方法一:copyText
<van-button @click="clickCopy()">复制</van-button>
clickCopy() {
this.$copyText("www.baidu.com").then(
res => {
console.log(res);
this.$toast("已成功复制,可直接去粘贴");
},
err => {
this.$toast("复制失败");
}
);
}
方法二 :v-clipboard:copy
<van-button
v-clipboard:copy="getCopyUrl()"
v-clipboard:success="onCopy"
v-clipboard:error="onError"
>复制</van-button
>
getCopyUrl() {
return "www.baidu.com";
}
onCopy(res: any) {
console.log(res.text);
this.$toast("已成功复制,可直接去粘贴");
}
onError(error: any) {
console.log(error);
this.$toast("复制失败");
}