Vue全局函数使用Promise出错?

这是定义的全局函数

import { Message } from 'element-ui'
export default {
  data() {
    return {
      p: Promise.resolve()
    }
  },
  install(Vue, options) {
    Vue.prototype.notify = (msg, type) => {
      this.p = this.p.then(this.$nextTick).then(() => {
        Message({
          message: msg,
          type: type
        })
      })
    }
  }
}

main.js

import global_ from './utils/message'

Vue.use(global_)

调用 Vue

 for (let i = 0; i < 10; i++) {
        this.notify('success' + i, 'success')
    }

报错信息

// -> 
TypeError: Cannot read property 'then' of undefined
    at VueComponent.Vue.notify (message.js:12)
    at VueComponent.onSubmit (index.vue?906b:147)
    at invokeWithErrorHandling (vue.runtime.esm.js:1854)
    at VueComponent.invoker (vue.runtime.esm.js:2179)
    at invokeWithErrorHandling (vue.runtime.esm.js:1854)
    at VueComponent.Vue.$emit (vue.runtime.esm.js:3882)
    at VueComponent.handleClick (element-ui.common.js:9362)
    at invokeWithErrorHandling (vue.runtime.esm.js:1854)
    at HTMLButtonElement.invoker (vue.runtime.esm.js:2179)
    at HTMLButtonElement.original._wrapper (vue.runtime.esm.js:6911)
阅读 3.6k
1 个回答
import { Message } from 'element-ui'
export default {
  p: Promise.resolve(),
  install(Vue, options) {
    Vue.prototype.notify = (msg, type) => {
      this.p = this.p.then(this.$nextTick).then(() => {
        Message({
          message: msg,
          type: type
        })
      })
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题