解决element 对话框重叠的问题

927 阅读1分钟

问题: 在一个方法中调用两次.$notify方法,会出现通知框重叠的问题。

export default {
  methods: {
    startHacking () {
      this.$notify({
        title: 'It works!',
        type: 'success',
        message: 'We\'ve laid the ground work for you. It\'s time for you to build something epic!',
        duration: 5000
      }),
      this.$nextTick();
       this.$notify({
        title: 'It works!',
        type: 'success',
        message: '奥利635664给!',
        duration: 5000
      })
    }
  }
}

原因:

let verticalOffset = options.offset || 0;
  instances.filter(item => item.position === position).forEach(item => {
    verticalOffset += item.$el.offsetHeight + 16;
  });
  verticalOffset += 16;
  instance.verticalOffset = verticalOffset;

计算通知的间距时,会取当前元素的高度 item.$el.offsetHeight ,但是因为vue的异步更新队列有缓冲机制,第一次方法调用时,并没有更新dom,导致取到的高度为0,所有第二个通知只是上移了默认的offset 16px,并没有加上第一个通知的高度

使用异步函数的方法

export default {
  methods: {
    startHacking:  async function() {
     await this.$notify({
        title: 'It works!',
        type: 'success',
        message: 'We\'ve laid the ground work for you. It\'s time for you to build something epic!',
        duration: 5000
      }),
     await this.$nextTick();
       this.$notify({
        title: 'It works!',
        type: 'success',
        message: '奥利635664给!',
        duration: 5000
      })
    }
  }
}