美文网首页
Clipboard中success重复提示

Clipboard中success重复提示

作者: 钱英俊真英俊 | 来源:发表于2018-08-20 15:53 被阅读0次

问题: 初次进入页面点击复制只会出现一次提示,页面切换只会再次点击会重复N次提示

解决方法
  • mounted 的时候绑定实例,destroyed 的时候销毁实例
methods: {
    copy () {
      if (JSON.stringify(this.clipboard) !== '{}') {
        this.clipboard.destroy()
      }
      let _this = this
      this.clipboard = new Clipboard('.copyText')
      this.clipboard.on('success', function (e) {
        console.log('success')
        _this.$Message.success('UID复制成功')
        e.clearSelection()
      })
      this.clipboard.on('error', function (e) {
        _this.$Message.success('复制失败')
        console.error(e)
      })
 },
  mounted: function () {
    this.copy()
  },
  destroyed: function () {
    this.clipboard.destroy()
  }
分析

因为在mounted的时候new 一个实例,再次进入页面时候又会new 一个实例,因此会出现多次提示。确保当前页面只有一个clipboard实例就可以了

相关文章

网友评论

      本文标题:Clipboard中success重复提示

      本文链接:https://www.haomeiwen.com/subject/rajliftx.html