这是定义的全局函数
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)