Element: [Bug Report] resetFields uses with v-if/v-else error [bug]

Created on 8 Jun 2018  ·  5Comments  ·  Source: ElemeFE/element

Element UI version

2.3.3

OS/Browsers version

Chrome 64.0.3282.186

Vue version

2.5.16

Reproduction Link

https://jsfiddle.net/s5ar1un3/39/

Steps to reproduce

  1. 点击'点我'按钮
  2. 选择'类型1'
  3. 关闭模态框
  4. 再次点击'点我'按钮,报错。

What is Expected?

期望resetFieldsformData.text还原为初始值。

What is actually happening?

resetFieldsformData.text还原为formData.value的初始值。

form

Most helpful comment

You need to key the form items: https://jsfiddle.net/s5ar1un3/41/

All 5 comments

Translation of this issue:

Element UI version
2.3.3

OS/Browsers version
Chrome 64.0.3282.186

Vue version
2.5.16

Reproduction Link
https://jsfiddle.net/s5ar1un3/39/

Steps to reproduce

  1. click the 'point me' button
  1. select 'type 1' '
  2. closed mode frame

  3. click the 'point me' button again and make a mistake.
    What is Expected?

Expect resetFields to restore formData.text to its initial value.
What is actually happening?

resetFields reduced formData.text to the initial value of formData.value.

form.vue

created() {
      this.$on('el.form.addField', (field) => {
        if (field) {
          this.fields.push(field);
        }
      }

      this.$on('el.form.removeField', (field) => {
        if (field.prop) {
          this.fields.splice(this.fields.indexOf(field), 1);
        }

      });
}

resetFields() {
        ...

        this.fields.forEach(field => {
          field.resetField();
        });
      },

form-item.vue

    mounted() {
        if (this.prop) {
            this.dispatch('ElForm', 'el.form.addField', [this]);
            ...

            let initialValue = this.fieldValue;

            ...

            Object.defineProperty(this, 'initialValue', {
              value: initialValue
            });
            ...
        }
    }

    resetField() {

        this.validateDisabled = true;
        if (Array.isArray(value)) {
          prop.o[prop.k] = [].concat(this.initialValue);
        } else {
          prop.o[prop.k] = this.initialValue;
        }
    }
  1. 主要是使用v-if/v-else时,由于数据变动,vue比较差异更新,组件已经变新的了,然鹅this.initialValue是旧组件值,所以resetFields后数据就是串的。
  2. 假设我有3个组件,2个不满足v-if,但是mounted阶段这两个组件是不满足if(this.prop)的,意思就是就算排除initialValue的问题,重置的也只会是一个组件。

You need to key the form items: https://jsfiddle.net/s5ar1un3/41/

if(this.prop) 无关,是因为切换 formData.class 时触发了其他 <el-form-item>beforeDestroy 钩子。可以在切换 formData.class 后重置表单:https://jsfiddle.net/s5ar1un3/42/

嗯嗯 发现了 所以撤销了后面的commit 谢谢你

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yorululu picture yorululu  ·  3Comments

FranzSkuffka picture FranzSkuffka  ·  3Comments

mochenxm picture mochenxm  ·  3Comments

makunsusu picture makunsusu  ·  3Comments

yubo111 picture yubo111  ·  3Comments