Home Web Front-end JS Tutorial Solution to the problem of vue router routing parameters disappearing after refreshing

Solution to the problem of vue router routing parameters disappearing after refreshing

Jun 28, 2017 pm 02:29 PM
router

This article mainly introduces vue-routerRouting parametersThe problem of refreshing and disappearing

Scenario: Single page application implemented by vue-router, login page call After logging in to the interface, the server returns the user information, and then passes it to the homepage component through router.push({name: 'index', params: res.data}), and displays the data on the homepage. But after refreshing the page, the data disappeared.

Solution:

1, session&Server rendering

The traditional solution is, The login page and the homepage are two separate pages. After successful login, the server generates a session corresponding to the user information, then renders the homepage data, and passes the sessionid to the browser through the response header and generates the corresponding cookie file. In this way, the next time the page is requested, the browser will bring the corresponding cookie in the http header, and then the server will determine whether the user is logged in based on the sessionid in the cookie, and then display the user data.

If the project adopts the idea of ​​front-end and back-end separation, and the server only provides interfaces and does not perform server rendering, then this method will not work.

2. $route.query

We can bring the login request parameters when routing:

router.push({name:'index', query:{username: 'xxx', password: 'xxxxxx'}})
...
this.$ajax({
 url: 'xxx',
 method: 'post',
 data: {
 username: this.$route.query.username,
 password: this.$route.query.password
 }
})
Copy after login

In this way, the login parameters will It is saved in the URL, like this: "http://xxx.xxx.xxx/index?username=xxx&password=xxxxxx", and then calls the login interface in the created hook to return the data.

Even if the password is md5 encrypted, it is definitely unreasonable to put sensitive information such as username and password in the URL.

3. Cookie

Another way is to store the login parameters in the cookie, then obtain the information stored in the cookie in the created hook, and then call the login interface. It is also unreasonable to store the user name and password in a cookie. The improved version is that the server returns a token after successful login, and the user data is obtained through the token within the validity period.

Cookie data access is more troublesome, because the key-value pairs in the cookie are strings and are linked by "=", and additional methods for operating cookies need to be written.

<script>
 function setCookie (name, value, exdays) {
 let date = new Date()
 date.setTime(date.getTime() + (exdays * 24 * 60 * 60 * 1000))
 let expires = "expires=" + date.toGMTString()
 document.cookie = name + "=" + value + "; " + expires
 }
 function getCookie (name) {
 name = name + "="
 let cookieArr = document.cookie.split(&#39;;&#39;)
 for (let i = 0; i < cookieArr.length; i++) {
  let cookie = cookieArr[i].trim()
  if (cookie.indexOf(name) === 0) {
  return cookie.slice(name.length)
  }
 }
 return ""
 }
Copy after login

4. HTML5 Web Storage

When it comes to Web storage, I must subconsciously think that many browsers do not support it. In fact, IE8 and above support localStorage and sessionStorage. The Vue project supports at least IE9, so you can use web storage with confidence.

There is no time limit for localStorage to store data, and it will not become invalid unless it is actively deleted. SessionStorage will become invalid when the page or browser is closed, which is suitable for this scenario.

We can store the token information in sessionStorage, and then request data through the token every time the page is refreshed; but since the token can be stored locally, why not just save commonly used data directly to the local? Utilizing local data can reduce client network requests and reduce server load.

Since localStorage and sessionStorage are read-only, they cannot be directly pointed to an object. You cannot use Object.assign() to copy the object, because the value will become the string "[object Object]", so you can only add attributes to sessionStorage through looping.

...
for (var key in res.data.customer) {
 sessionStorage[key] = res.data.customer[key]
} 
...
Copy after login

The above are the problems I encountered in my recent work. The final solution was to use sessionStorage to store data. If you have a better solution, please feel free to enlighten me.

That’s it. All the content, I hope it will be helpful to everyone's study, and I also hope everyone will support Script Home.

The above is the detailed content of Solution to the problem of vue router routing parameters disappearing after refreshing. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

React Router User Guide: How to implement front-end routing control React Router User Guide: How to implement front-end routing control Sep 29, 2023 pm 05:45 PM

ReactRouter User Guide: How to Implement Front-End Routing Control With the popularity of single-page applications, front-end routing has become an important part that cannot be ignored. As the most popular routing library in the React ecosystem, ReactRouter provides rich functions and easy-to-use APIs, making the implementation of front-end routing very simple and flexible. This article will introduce how to use ReactRouter and provide some specific code examples. To install ReactRouter first, we need

The unique advantages of Vue Router Lazy-Loading routing, how to optimize page performance? The unique advantages of Vue Router Lazy-Loading routing, how to optimize page performance? Sep 15, 2023 am 10:36 AM

VueRouter is a routing management plug-in officially provided by Vue.js. It can help us implement page navigation and route switching in Vue applications. The Lazy-Loading feature is a unique advantage of VueRouter, which can greatly optimize page performance. In this article, we will introduce VueRouter’s Lazy-Loading routing feature and provide some practical code examples for optimizing page performance. Lazy-Loading means when needed

What should I do if react router does not display? What should I do if react router does not display? Dec 30, 2022 am 09:30 AM

Solution to react router not displaying: 1. Add browserRouter to the parent routing component to wrap the router; 2. Use "this.props.history.go()" to refresh the component; 3. Add "forcerefresh" to the browserrouter parameter ={true}"; 4. Write a hook function in "<Route>" and call it when leaving or entering this route.

Vue Router Lazy-Loading routing: a trend to help improve page performance Vue Router Lazy-Loading routing: a trend to help improve page performance Sep 15, 2023 am 08:03 AM

VueRouter is the official route manager in the Vue.js framework. It allows developers to switch page content through route mapping, making single-page applications more controllable and easier to maintain. However, as applications become more complex, the loading and parsing of routes can become a performance bottleneck. In order to solve this problem, VueRouter provides a function of lazy loading of routes, which defers the loading of routes until actually needed. Lazy-loading is a loading technology that

How to use Router to implement redirection function in Vue project How to use Router to implement redirection function in Vue project Sep 15, 2023 am 08:36 AM

How to use Router to implement redirection function in Vue project In a Vue project, we often need to implement jump and redirection functions between pages. And VueRouter provides a simple yet powerful solution. This article will introduce how to use Router to implement the redirection function in the Vue project and give specific code examples. Install VueRouter First, we need to install VueRouter in the Vue project. Can

How to use router routing to implement jump parameters in vue3 How to use router routing to implement jump parameters in vue3 May 16, 2023 am 10:49 AM

1. Route jump 1. First introduce the API—useRouterimport{useRouter}from'vue-router'2. Define the router variable on the jump page // First define constrouter=useRouter() in the setup 3. Use router.push to jump to the page //String router.push('home')//Object router.push({path:'home'})//

How is lazy loading of routes implemented in Vue Router? How is lazy loading of routes implemented in Vue Router? Jul 21, 2023 am 10:40 AM

How is lazy loading of routes implemented in VueRouter? In Vue development, we usually use VueRouter to implement jumps and routing control between pages. When the project becomes large, we may have many routing pages to load, which will cause the entire project to load slowly. In order to improve the performance of the project, VueRouter provides a mechanism for lazy loading of routes. Lazy loading of routes means that the routing page will be loaded only when it is accessed, instead of loading all routes when the application is initialized.

Examples of Vue Router route redirection and alias settings Examples of Vue Router route redirection and alias settings Aug 10, 2022 pm 02:16 PM

This article will introduce Vue Router route redirection and alias settings through examples. I hope it will be helpful to friends in need!

See all articles