版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_36422236/article/details/83419093
最近做项目的时候发现element-ui 的messageBox 没有自动关闭的api,于是自己写了个,直接上代码
window.$alert = (txt='哈哈哈哈哈',timer='2000') => {
let str = `<p>${txt}</p>`;
this.$alert(str, '', {
confirmButtonText: '确定',
dangerouslyUseHTMLString: true,
customClass: 'myAlert',
lockScroll: false,
confirmButtonText: '知道了',
callback: action => {
this.$message({
type: 'info',
message: `action: ${ action }`
});
}
});
return new Promise((resolve,reject) => {
setTimeout(() => {
$('.myAlert').find('.el-button').trigger('click');
resolve();
},timer)
})
}
用的时候借助了trigger方法,需要传两个参数,txt是要显示的文本,timer是多长时间消失。
返回用到了promise,可以在弹框消失之后再进行你想要的操作,
比如:
window.$alert('success',1000).then(() => {
console.log(1)
}).catch(() => {
console.log(2)
})