Vue opens a new window and implements a graphic example of parameter transfer

Vue opens a new window and implements a graphic example of parameter transfer

The function I want to achieve is to open a new window to display a new page, and parameters need to be passed, and the parameters cannot be displayed in the address bar. Moreover, when I refresh the page, the passed parameters cannot be lost and must always exist unless I manually close the new window, that is, the browser tab.

Through programming for Baidu, I found that the online content can't achieve this effect at all, and there are pitfalls. It is obviously impossible to achieve, but they are written as if it is a matter of fact. So I deliberately added "There are pictures and truth" in the title. I am not deceiving you. If you can't achieve the function, don't come out to fool people.

I will first write out the code I have made, and then introduce the pitfalls that others cannot implement, as well as the things that need to be paid attention to in this regard.

Open a new window and pass the code

//Open a new window and pass parameters. The parameters cannot be displayed in the address bar. If the window is not closed, it will be refreshed continuously. The parameters are always valid. handleWindow() {
 //Mainly realize the function of opening a new window var route = this.$router.resolve({
 name: 'Xterm',
 })
 //Mainly implement the function of storing parameters sessionStorage.setItem("ip", "1.1.1.1");
 
 window.open(route.href, '_blank')
}

My routing configuration is also posted.

{
 path: '/xterm',
 hidden: true,
 component: () => import('@/views/monitor/xterm/index'),
 name: 'Xterm',
},

Now the page receives parameters

//Vue initialization method, once the page is initialized, get the parameter created() {
 alert(sessionStorage.getItem("ip"));
 this.ip = sessionStorage.getItem("ip")
},

Post a truth picture

This is done by opening a new window through vue and passing parameters through sessionStorage. Why can only this method be used to achieve this function? Let me tell you about my experience of stepping into the pit. There are bad people among the people who use fake codes to cheat people on the Internet. Which programmer cannot withstand such a test?

First of all, this function requires opening a new window, so there are 3 ways to do it;

The first method: through the <router-link> tag. I personally don’t like the way this tag is written. It’s barely OK for simple business scenarios, but I have to deal with a lot of business and many parameters. This way of writing tags is not conducive to business expansion. Exclude directly, OUT!

The second method: through router.replace method. This method opens the window and replaces the current A page with the B page to jump to. I open the new window to see new things, but it directly makes my previous page disappear. Isn't this fucked up? After my experiment, OUT!

The third method: through the router.resolve method, which is to open a new window without affecting the current A page, and directly open a browser tab. I can switch tabs back and forth to view the information data of the two pages. This is nice, I like it, this is it.

By the way, after vue2.0, in order to keep consistent with the increasingly popular HTML5, router.go and router.push no longer support the attribute of opening a new window. router.go is used as forward and backward; router.push is used as a guide to a special page. This jump will not open a new window, but has a history function. If you have multiple tabs in one window, you can use this to open multiple tabs, as shown in the figure;

Okay, using router.resolve, we can jump to the new window, so now we start passing parameters, and this is where it gets tricky. According to the online transfer, there are mainly two types of parameter transmission:

//Test the first parameter testA (aaa) {
 const route = this.$router.resolve({
   name: 'Xterm', 
   params: {
     ip:aaa
   }
 })
 window.open(route.href,'_blank')
}

//Test the second parameter testA (bbb) {
 const route = this.$router.resolve({
   path: '/xterm', 
   query: {
     ip: bbb
   }
 })
 window.open(route.href,'_blank')
}

The corresponding receiving parameters are as follows;

created() {
 //Test the first access parameter this.ip = this.$route.params.ip
 //Test the second access parameter this.ip = this.$route.query.ip
},

The test result is that the first type can never get the value, while the second type exposes the values ​​of the passed parameters in the address bar. Even if the first method undergoes all kinds of special processing, the best result is only getting the value for the first time. If you refresh the page again, the value will be lost. How can this be possible? Who can guarantee that users won’t feel a bit stuck and click the refresh button? There is no problem at all in passing parameters in router.push . All parameters can be received. The difference is that for the name+params combination, the parameters will not be in the address bar and the data will disappear after refreshing. For the path+query combination, the parameters will be in the address bar and the values ​​will remain there no matter how you refresh the page. I guess after trying it with push, I naturally thought that resolve would be the same. I've seen many blogs saying that they don't pass parameters through the address bar, but use the first method, and then they never succeed, but the number of clicks is still very high, which makes me want to cry but have no tears.

There was no choice but to do it myself. I looked for cache and found localStorage and sessionStorage properties. SessionStorage is used to temporarily save data, which will be deleted after closing the window or tab; localStorage is used to permanently save the data of the entire website. The saved data has no expiration time until it is manually deleted. Obviously, sessionStorage will do.

The browser versions that support these two attributes are as follows;

At this point, the problem has actually been solved. In fact, it is not complicated. It’s just that the answers on the Internet are of varying quality. I’ll record them for others to use.

Summarize

This is the end of this article about Vue opening a new window and passing parameters. For more relevant Vue opening a new window passing parameters content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • electron+vue realizes div contenteditable screenshot function
  • Steps to encapsulate the carousel component in vue3.0
  • Vue implements sample code for dragging files from desktop to web page (can display pictures/audio/video)
  • Vue basic instructions example graphic explanation
  • Vue imitates Ctrip's carousel effect (sliding carousel, highly adaptive below)
  • vue+echarts realizes the flow effect of China map (detailed steps)
  • An example of refactoring a jigsaw puzzle game using vue3
  • How to use bar charts in Vue and modify the configuration yourself
  • Vue implements irregular screenshots

<<:  MySQL 5.7.18 winx64 installation and configuration method graphic tutorial

>>:  Summary of the switching problem and solution of installing multiple JDK versions in win10 64-bit system

Recommend

How to set up automatic daily database backup in Linux

This article takes Centos7.6 system and Oracle11g...

A brief talk about the diff algorithm in Vue

Table of contents Overview Virtual Dom principle ...

Why developers must understand database locks in detail

1.Lock? 1.1 What is a lock? The real meaning of a...

Vue keeps the user logged in (various token storage methods)

Table of contents How to set cookies Disadvantage...

JavaScript+html to implement front-end page sliding verification (2)

This article example shares the specific code of ...

mysql 5.7.18 winx64 password change

After MySQL 5.7.18 is successfully installed, sin...

Implementation of mounting NFS shared directory in Docker container

Previously, https://www.jb51.net/article/205922.h...

A nice html printing code supports page turning

ylbtech_html_print HTML print code, support page t...

How to use axios request in Vue project

Table of contents 1. Installation 2. There is no ...

The principles and defects of MySQL full-text indexing

MySQL full-text index is a special index that gen...

Detailed explanation of nginx-naxsi whitelist rules

Whitelist rule syntax: BasicRule wl:ID [negative]...

Summary of 10 advanced tips for Vue Router

Preface Vue Router is the official routing manage...

How to connect to docker server using ssh

When I first came into contact with docker, I was...