element-ui messagebox 自动关闭
2022-10-08 17:22:18
269
{{single.collect_count}}

最近做项目的时候发现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)})
回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}