返回 Promise 且被 reject 是什么意思

我在切换选项卡的时候会根据用户的选择来决定是否切换选项卡,选项卡用的是elemenui的tabs组件,我看文档中有一个before-leave的方法如图:

clipboard.png

我的代码:

this.$confirm('您还未保存简介,确定需要离开吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            return true;
        }).catch(() => {
            return false
        });

当我选择取消的时候会returnfalse,但是这样并没有阻止切换,于是我隐隐感觉要用文档中的这句话来阻止“返回 Promise 且被 reject,则阻止切换。”,
但是实在不知道怎么写,求指教

阅读 10k
3 个回答

直接把$confirm return回去试下,因为$confirm本来就是返回一个Promise

return this.$confirm('您还未保存简介,确定需要离开吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        })

试试这个:

return new Promise((res,rej)=>{
    this.$confirm('您还未保存简介,确定需要离开吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            res()
        }).catch(() => {
            rej()
        });
}
        )

你应该返回一个 promise

return new Promise(resolve, reject) {

this.$confirm('您还未保存简介,确定需要离开吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            resolve()
        }).catch(() => {
            reject()
        });
}

试试吧

推荐问题