vue+element-ui,打开页面会有一个弹框,不能设置定时关闭只能手动关闭,跳转其他页面时如何让这个弹框自动关闭

vue+element-ui,打开页面会有一个弹框, 给的需求是不能设置定时关闭只能手动关闭,跳转其他页面时如何让这个弹框自动关闭

图一是在首页页面时

img

图二是点击其他页面时也会有这个弹框

img

代码部分(message要展示部分 前面已经定义好了)

img

页面是否被 keep-alive包裹了,是的话要在 deactivated 这个生命周期里边销毁

duration: 0 这个代表自动关闭的事件 单位是毫秒 写成0表示 不自动关闭

通过beforeRouteLeave在页面跳转前关闭

data () {
  return {
    notifyIns: null
  }
},
beforeRouteLeave (to, from, next) {
  this.notifyIns && this.notifyIns.close()
  next()
},
created () {
  this.notifyIns = this.$notify({
    title: '提示',
    message: '这是一条不会自动关闭的消息',
    duration: 0
  })
}

或者在页面销毁前生命周期关闭

beforeDestroy () {
  this.notifyIns && this.notifyIns.close()
}