简体   繁体   English

在 vue 中重新加载/刷新时保留表单中的填充字段

[英]Retain filled fields in the form on reload/refresh in vue

I want to add the feature that the filled form field values are retained in the form on page reload.我想添加在页面重新加载时将填写的表单字段值保留在表单中的功能。 Is there a way to to this.有没有办法做到这一点。 Will vm.$forceUpdate() solve this issue? vm.$forceUpdate()会解决这个问题吗? Please help me solve this issue请帮我解决这个问题

   <template>
     <v-card class="mb-12">
       <v-form :model='user' class="content-padding" ref='pdfInputs'>
            <div class="section-header">
              User
            </div>
            <v-container fluid>
              <v-layout row wrap>
                <v-flex xs12 md6 class="add-col-padding-right info-align">
                  <v-text-field required
                    label='User Full Name​'
                    v-model='user.user_full_name'>
                  </v-text-field>
                </v-flex>
                <v-flex xs12 md6 class="add-col-padding-left info-align">
                  <v-text-field
                    label='Street Address'
                    v-model='user.user_address'
                    required>
                  </v-text-field>
                </v-flex>
              </v-layout>
            </v-form>
         </v-card>
     </template>  


<script>

export default {
  data () {
    return {
      user: {
        user_full_name: '',
        user_address: []
      }
    }
  }
}
</script>

You can use localStorage or indexedDB for persistent storage.您可以使用localStorageindexedDB进行持久存储。

You should either watch for changes on user or stop using v-model and use v-bind and v-on:change to be able to catch changes to your models and save them.您应该watch user的更改或停止使用v-model并使用v-bindv-on:change以便能够捕获对模型的更改并保存它们。

Then on mounted or other lifecycle methods you need to read from your persistent medium and populate user .然后在mounted或其他生命周期方法上,您需要从持久介质中读取并填充user

Going forward, if you encounter this need more and more, you could use vuex to store your data and use the mutations to save to persistent medium.展望未来,如果您越来越多地遇到这种需求,您可以使用vuex来存储数据并使用mutations保存到持久性介质。 You should also populate the store at startup from localStorage/indexedDB.您还应该在启动时从 localStorage/indexedDB 填充存储。

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

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