vue + elementui 项目中使用clipboard2 复制标题时提示this.$messsage is not a function

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(' 该浏览器不支持自动复制')
      })
    },

此时报错
在这里插入图片描述


版权声明:本文为u010328533原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。