简体   繁体   English

如何在 Vue.js 中使用带有嵌套属性的 v-model

[英]How to use v-model with nested properties in Vue.js

I have created a form and I'm trying to understand how to use v-model with nested properties in Vue.js我创建了一个表单,我试图了解如何在 Vue.js 中使用带有嵌套属性的 v-model

This is the code for my template, as you can see I'm trying to reference the nested properties like so: form.dobDate , but how do I reference the properties ( dobDate , dobMonth and dobYear ) in mounted and watch ?这是我的模板代码,你可以看到我想引用类似这样的嵌套属性:form.dobDate,但我怎么在安装腕表参考属性(dobDate,dobMonthdobYear)? So that whatever I type remains there on page refresh?这样我输入的任何内容都会在页面刷新时保留在那里?

<template>
  <div>
    <b-form novalidate>
      <b-form-select name="dobDate" id="dobDate" v-model="form.dobDate" :options="optionsDays"></b-form-select>

      <b-form-select name="dobMonth" id="dobMonth" v-model="form.dobMonth" :options="optionsMonths"></b-form-select>

      <b-form-input
        placeholder="Year of Birth"
        required
        autofocus
        class="form-control"
        name="year"
        id="year"
        min="0"
        max="2003"
        type="number"
        v-model="form.dobYear"
      ></b-form-input>

      <input type="text" v-model="name" />

      <b-button type="submit" variant="primary">Submit</b-button>
    </b-form>
    <b-card class="mt-3" header="Form Data Result">
      <pre class="m-0">{{ form }}</pre>
    </b-card>
  </div>
</template>

Here is script code:这是脚本代码:

<script>
export default {
  data() {
    return {
      name: "",
      form: {
        dobDate: {
          selected: ""
        },
        dobMonth: {
          selected: ""
        },
        dobYear: "",
        name: ""
      },
      optionsMonths: [
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"
      ],
      show: true
    };
  },

  mounted() {
    if (localStorage.name) {
      this.name = localStorage.name;
    }
  },
  watch: {
    name(newName) {
      localStorage.name = newName;
    },
    deep: true
  },

  computed: {
    optionsDays() {
      return Array.from(Array(31).keys());
    }
  },

  methods: {
    onSubmit(evt) {
      evt.preventDefault();
      alert(JSON.stringify(this.form));
    },
    getDates() {
      return Array.from(Array(31).keys());
    }
  }
};
</script>

As you can see I have a form object, with nested properties inside it which I want to bind to, and I'm also using mounted () and watch to store the data in local storage for when the page refreshes or when submitting the form (no validation at the moment).如您所见,我有一个表单对象,其中包含我想要绑定的嵌套属性,并且我还使用mount () 并观察将数据存储在本地存储中,以便在页面刷新或提交表单时使用(目前没有验证)。

You could watch the form deeply (accessing its nested field ) and loop through it values using :您可以深入观察form (访问其嵌套字段)并使用以下方法遍历它的值:

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

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

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