Home >Web Front-end >Front-end Q&A >How to solve the problem of Vue Router not retaining the original page

How to solve the problem of Vue Router not retaining the original page

PHPz
PHPzOriginal
2023-04-26 14:35:291283browse

Vue Router is a routing management solution officially provided by Vue.js, which can realize page jumps and dynamic loading of components in single-page applications (SPA). The importance of Vue Router is self-evident, it can provide better user experience and efficiency for projects.

But when using Vue Router, we may encounter a problem: when jumping to a page, the original page will be destroyed, and the original state and data cannot be retained. This situation usually occurs when using vue-router to perform cross-domain page jumps, such as jumping from one domain name or subdomain name to a page in another domain name or subdomain name. At this time, the route reaches from one Web page to another, the entire page needs to be reloaded, and the previous state and data will be lost.

So, how to solve the problem of Vue Router not retaining the original page? The following are some solutions:

  1. Using Keep-Alive components

Vue.js provides the Keep-Alive component, which can cache already loaded component instances. They will be re-rendered and destroyed. Use the Keep-Alive component to retain the status and data of the original page when the page jumps.

Add the Keep-Alive component to the component that needs to retain state and data, as shown below:

<template>
  <div>
    <keep-alive>
      <router-view></router-view>
    </keep-alive>
  </div>
</template>

In this way, the component state and data can be retained, and the page jump effect can be achieved at the same time.

  1. Using Storage

If you don’t want to use the Keep-Alive component, you can also use Storage to store page status and data. Listen to the route jump life cycle function beforeRouteLeave in the component and store the state and data that need to be saved in Storage; in the created life cycle function after the route jump, read the state and data from Storage and restore them to the component.

The specific code is implemented as follows:

// 路由跳转前保存组件状态和数据到Storage
beforeRouteLeave(to, from, next) {
  localStorage.setItem('data', JSON.stringify(this.data));
  next();
}
// 路由跳转后从Storage中读取组件状态和数据并赋值给组件
created() {
  let data = JSON.parse(localStorage.getItem('data'));
  if (data) {
    this.data = data;
  }
}

This way, the status and data of the original page can be retained when the page jumps.

  1. Using Vuex

Vuex is a state management framework for Vue.js, used to share state among multiple components. If you need to retain the page state and data, you can use Vuex to store the page state and data in the global state, and then read it from the global state and assign it to the component after the page jumps.

The specific code implementation is as follows:

// 在Vuex Store中定义状态和数据
const state = {
  data: {}
}
const mutations = {
  setData(state, data) {
    state.data = data;
  }
}
// 在组件中引入Vuex和Store,并将数据存储在全局状态中
import { mapMutations } from 'vuex';
export default {
  methods: {
    ...mapMutations(['setData']),
    beforeRouteLeave(to, from, next) {
      this.setData(this.data);
      next();
    }
  },
  created() {
    this.data = this.$store.state.data;
  }
}

This way, the status and data of the original page can be retained through Vuex when the page jumps.

Summary:

The above are several common solutions to the problem that Vue Router does not retain the status and data of the original page. They each have their own advantages and disadvantages, and you can choose the appropriate solution according to specific business needs. Whether it is Keep-Alive components, Storage or Vuex, they can help us retain the original state and data when page jumps, improving user experience and efficiency.

The above is the detailed content of How to solve the problem of Vue Router not retaining the original page. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Is vue static by default?Next article:Is vue static by default?