vue.js 防止清除输入的第一页的多步形式

4sup72z8  于 2023-03-03  发布在  Vue.js
关注(0)|答案(1)|浏览(115)
    • 我有一个多步骤表单,我想知道在第一个表单页面填写完输入后,当我从第二个表单页面切换到第一个表单页面时,如何防止它被清除。现在输入文本消失了。或者我应该以某种方式使用localStorage?

https://codesandbox.io/s/intelligent-jasper-teh1im?file=/src/components/Step1.vue:0-1188* *

<template >
  <div class="container">
    <div class="content-wrapper">
      <h1>Step 1</h1>
      <div class="form-group">
        <label>First Name</label
        ><input
          name="name"
          v-model="firstName"
          placeholder="Your first name"
          class="form-control"
          required
        />
      </div>
      <div class="form-group">
        <label>Last Name</label
        ><input
          name="lastname"
          v-model="lastName"
          placeholder="Your last name"
          class="form-control"
          required
        />
      </div>
      <!-- @click.prevent="goToStep(2)" -->
      <button type="button" @click.prevent="nextStep" class="btn">Next step</button>
    </div>
    <Button />
  </div>
</template>

<script>
import Button from "./Button.vue";
export default {
  components: {
    Button,
  },
  methods: {
    nextStep() {
      this.$emit("next");
    },
  },
};
</script>
e4eetjau

e4eetjau1#

只需使用<KeepAlive>组件,这样动态组件就不会被卸载。

<template>
  <KeepAlive>
    <component :is="currentStep" />
  </KeepAlive>
</template>

相关问题