简体   繁体   English

从新页面返回时,Vue.js 表单页面丢失数据

[英]Vue.js form page loses its data when coming back from a new page

I have a Vue.js page that is basically one big textarea.我有一个 Vue.js 页面,它基本上是一个大的 textarea。 If the user taps "Settings" on that page and opens the Settings page, then goes back to the original page, the textarea data is all gone.如果用户在该页面点击“设置”并打开“设置”页面,然后回到原始页面,则textarea数据全部消失。 Surely there must be an easy way of not losing everything without saving it to a database or something, right?当然,必须有一种简单的方法不会丢失所有内容而不将其保存到数据库或其他东西,对吗?

I'm using Vue Router.我正在使用 Vue 路由器。

Does Vue Router have something like a modal window (but for use in a smart phone app) that can be dismissed so everything underneath is saved? Vue Router 是否有类似模态窗口(但用于智能手机应用程序)之类的东西,可以关闭它以便保存下面的所有内容?

I would encode the form ID in the route path, and set props=true so that the ID is passed to the component's prop:我会在路由路径中对表单 ID 进行编码,并设置props=true以便将 ID 传递给组件的 prop:

// router.js
export default new VueRouter(
  routes: [
    {
      path: "/form/:id",
      name: "form",
      component: () => import("@/components/MyForm.vue"),
      props: true
    }
  ]
)

// MyForm.vue
export default {
  props: {
    id: {
      type: [Number, String],
      default: 0,
      required: true,
    },
  },
  data() {
    return {
      text: "The text " + this.id,
    };
  },
}

Then use <keep-alive> to reuse cached views , and use the route's full path as the key for router-view :然后使用<keep-alive>重用缓存的 views ,并使用路由的完整路径作为router-viewkey

<keep-alive>
  <router-view :key="$route.fullPath" />
</keep-alive>

demo 演示

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

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