账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    iview表单验证promise失效?
    44
    0

    问题已解决,原因是我没有正确调用回调

    同样的代码,放到mounted里面有效,放到处理程序中就无效了。路过的大神帮忙看看?

    mounted () {  // 能正确输出
        this.$refs.loginForm.validate(() => {
            console.log('hi')
        }, () => {
            console.log('error')
        })
    }
    // -------------------------------------------
    toLogin () {
        console.log('start')  // 'start'
        
        // no console...
        this.$refs.loginForm.validate(() => {
            console.log('hi')
        }, () => {
            console.log('error')
        })
        
        console.log('end')  // 'end'
    }
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 用钱买、真情 普通会员 1楼

      iview表单验证时如果 promise 失效,可能的原因有以下几点:

      1. 确保在 mounted 生命周期钩子中调用了 Promise 的 resolve 方法:如果你在 mounted 生命周期钩子中调用了 Promise 的 resolve 方法,那么 promise 就会被正常处理。否则,promise 会在页面被销毁后重新生成,导致 Promise 失效。

      javascript mounted() { this.$axios.get('your-api-url') .then(response => { this.$refs.myForm.value = response.data; this.$axios.get('your-Another-api-url') .then(response => { // 处理第二个 API 的响应 }) .catch(error => { // 处理第二个 API 的错误 }); }) .catch(error => { // 处理第一个 API 的错误 }); }

      1. beforeDestroy 生命周期钩子中调用了 Promise 的 reject 方法:如果你在 beforeDestroy 生命周期钩子中调用了 Promise 的 reject 方法,那么 promise 就会被强制终止。这样可以防止 Promise 失效,但也会导致组件的销毁。

      javascript beforeDestroy() { this.$axios.get('your-api-url') .then(response => { this.$refs.myForm.value = response.data; this.$axios.get('your-Another-api-url') .then(response => { // 处理第二个 API 的响应 }) .catch(error => { // 处理第二个 API 的错误 }); }) .catch(error => { // 处理第一个 API 的错误 }); }

      1. 使用 Promise 的 catch 方法处理错误:如果你的 promise 中有 catch 方法,那么你可以直接处理这个 catch 方法中的错误。这样可以确保在 promise 失效时,组件不会被销毁。

      javascript axios.get('your-api-url') .then(response => { this.$refs.myForm.value = response.data; this.$axios.get('your-Another-api-url') .then(response => { // 处理第二个 API 的响应 }) .catch(error => { // 处理第二个 API 的错误 }); }) .catch(error => { // 处理第一个 API 的错误 });

      如果你的 promise 失效是由于代码错误导致的,那么你可能需要找到并修复错误,而不是试图解决 Promise 的问题。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部