Antd for Vue Form报错You cannot set a form field before render

vue 11-13 11:15

使用Antd for Vue Form组件赋值 setFieldsValue时报错:

Warning: You cannot set a form field before rendering a field associated with the value. You can use `getFieldDecorator(id, options)` instead `v-decorator="[id, options]"` to register it before render.

相关问题解决方法:vue框架form表单赋值setFidesValue不生效问题。也查过网友们说的报错的原因:

this.form.setFieldsValue 传值的时候只能是 form 中用到的参数(即是 getFieldDecorator 方法中的 field )没有的 field 一律不允许多传,否则就会报错。

很奇怪,另一个页面使用相同的代码完全没有问题,因之前碰到过这个问题,检查过后完全没有问题。因为代码是直接复制另一个页面修改的,那就用笨办法,代码对比,这一看过后发现了问题。原来是新页面添加的setTimeout 写法有问题。错误的写成为 setTimeout(this.form.setFieldsValue({ name:this.createForm.name}},300) 难怪,看来这代码可是一点都不能粗心哈

setTimeout的正确写法

setTimeout(要执行的代码, 等待的毫秒数)
setTimeout(JavaScript 函数, 等待的毫秒数)

例如上面的就要修改成正确的:

setTimeout(()=>{this.form.setFieldsValue({ name:this.createForm.name}}},300)。完美解决!

相关推荐