vue + elementui 项目中使用clipboard2
1.安装依赖包
npm install vue-clipboard2 --save
2.在项目main.js 文件中引入
import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)
3.具体页面中使用
// 复制标题到剪贴板
titleCopy(row) {
const _this = this
this.$copyText(`${row.code} ${row.name}`).then(function(e) {
_this.$message.success(' 已复制到剪贴板!')
console.log(e, '-----------')
}, function(e) {
_this.$message.warning(' 该浏览器不支持自动复制')
})
},
其中的关键是
const _this = this
错误示范
第一次使用时,按照官方文档只写了此代码
// 复制标题到剪贴板
titleCopy(row) {
this.$copyText(`${row.code} ${row.name}`).then(function(e) {
this.$message.success(' 已复制到剪贴板!')
console.log(e, '-----------')
}, function(e) {
this.$message.warning(' 该浏览器不支持自动复制')
})
},
此时报错