iview表单验证promise失效?

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

同样的代码,放到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'
}
阅读 4.1k
1 个回答

使用this.$ref获取元素必须等到元素或组件加载完成后才可获取到,也就是在mounted周期后。

注意 mounted 不会承诺所有的子组件也都一起被挂载。如果你希望等到整个视图都渲染完毕,可以用 vm.$nextTick 替换掉 mounted:
mounted: function () {
  this.$nextTick(function () {
    // Code that will run only after the
    // entire view has been rendered
  })
}
推荐问题