简体   繁体   English

如何重构重复的条件Vue.js代码?

[英]How can I refactor repetitive conditional Vue.js code?

I have this code in my Vue.js component: 我的Vue.js组件中包含以下代码:

mounted() {
    if (localStorage.dobDate) {
      this.form.dobDate = localStorage.dobDate;
    }

    if (localStorage.dobMonth) {
      this.form.dobMonth = localStorage.dobMonth;
    }

    if (localStorage.dobYear) {
      this.form.dobYear = localStorage.dobYear;
    }
  },

  watch: {
    "form.dobDate": {
      handler: function(after, before) {
        localStorage.dobDate = after;
      },
      deep: true
    },
    "form.dobMonth": {
      handler: function(after, before) {
        localStorage.dobMonth = after;
      },
      deep: true
    },
    "form.dobYear": {
      handler: function(after, before) {
        localStorage.dobYear = after;
      },
      deep: true
    }

Ask you can see it can get very repetitive, if for example I had a large form, and I don't want to do this for every field. 询问您是否可以看到它重复性很强,例如,如果我的表格很大,并且我不想在每个字段中都这样做。 Is there a way I can approach this to make it more DRY? 有什么办法可以使它变得更干吗? Is there a way I can make it more dynamic for any field in a form for example? 例如,是否可以通过某种方式使表单中的任何字段更具动态性?

In the mounted hook create an array of localStorage fields ["dobDate","dobMonth","dobYear"] and loop through it using forEach method, for each field localStorage[fieldName] check if it's defined using conditional operator , if it's defined assign it to the correspondant field name in the form data property else pass to the next element: 在已挂接的挂钩中,创建一个数组localStorage字段[“ dobDate”,“ dobMonth”,“ dobYear”],并使用forEach方法遍历该数组,对于每个字段localStorage[fieldName]请检查是否使用conditional operator定义了该字段,是否已定义assign将其传递给form数据属性中的对应字段名称,否则传递给下一个元素:

mounted(){
["dobDate","dobMonth","dobYear"].forEach(field=>{

localStorage[field]?this.form[field]=localStorage[field]:{};

})

}

In the watch property watch the form object deeply (watch its nested fields) then loop through its keys by doing the reciprocal operation made in mounted hook : watch属性中,深入观察form对象(观察其嵌套字段),然后通过执行在已安装的hook中进行的倒数操作,遍历其键:

 watch: {
    form: {
      handler: function(after, before) {
        Object.keys(after).forEach(key=>{
          localStorage[key]=after[key]
       })
      },
      deep: true
    }
   }

Here is another approach with multiple (no deep) watchers. 这是具有多个(无深度)观察者的另一种方法。

data: {
  form: {},
  dateFields: ['dobDate', 'dobMonth', 'dobYear']
},

mounted() {
  for (const dateField of this.dateFields) {
    if (localStorage[dateField])
      this.$set(this.form, dateField, localStorage[dateField])
  }
},

created() {
  for (const dateField of this.dateFields) {
    this.$watch('form.' + dateField, function(after, before) {
      localStorage[dateField] = after;
    });
  }
}

I ignore if it's more or less efficient than only one deep watcher. 我忽略了它是否比仅一个深度监视者有效或高或低。 It may depends on the way your data change. 这可能取决于您的数据更改方式。

I'm sure you must have reasons for using localStorage for saving form data in localStorage, so with this code, you can pass the whole form object to localStorage and can retrieve that. 我确定您必须有理由使用localStorage将表单数据保存在localStorage中,因此使用此代码,您可以将整个表单对象传递给localStorage并进行检索。 in this case, any change in form would make this watch run 在这种情况下,任何形式上的更改都会使该手表运行

mounted() {
    if (localStorage.form) {
      this.form = localStorage.form
    }
  },

 watch: {
    "form": {
      handler: function(after, before) {
        localStorage.form = after;
      },
      deep: true
    }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM