Home Web Front-end JS Tutorial Vue mobile terminal routing switching case analysis

Vue mobile terminal routing switching case analysis

May 21, 2018 pm 03:20 PM
switch Case parse

This time I will bring you an analysis of the vue mobile routing switching case. What are the precautions for vue mobile routing switching? The following is a practical case, let's take a look.

The most important of them are the following two problems:

Switching of the browser navigation bar


When sliding to switch on IOS, there will be two page transitions. Field animation, a switch when it slides, and then triggers the transition animation we set.


Except for the above two questions, the rest of the operations can be set within the page and are basically controllable. Mainly to solve the above two problems.

You can see the actual written effect: Online DEMO

1. Switching the browser navigation bar

By recording history To compare and judge whether to go forward or backward

The following example


A page-> B page-> C page


If I go from page A to page B and then to page C, there will be 3 historical records

We use an array to represent: ['/a', '/b', '/c']

Then when I click the back button on the browser navigation bar, I will return to page B.

At this time, I only need to determine whether page B exists. If it exists, it proves that I clicked the back button. .

Then as long as I go back, I can click the browser's forward button. How to judge whether it is moving forward at this time?

We can do this.

When we go back to page B, doesn’t the history record still save the three paths ['/a', '/b', '/c']?

We can delete page B The following path is now ['/a', '/b'];

If we go back to page A, then the path we save is ['/a']

As long as we click For the forward button, let's look for it in the saved path. If we can't find the path, then the forward judgment will be completed.

The above is a normal situation.

But what if we enter some pages repeatedly.

Just like the following situation

A page-> B page-> C page-> B page-> C page

Now we take 5 steps and reach the second C page, then we take a step back and reach the B page


The problem comes out at this time, we delete the first B page The path is still to delete the path after the second B page

Let's first try to delete the path of the second B page, then the path we still save is: ['/a', '/b', '/c', '/b'].


1. At this time, we operate according to the logic of the normal situation above.


I click forward, and then I search in the saved path. If I can’t find it, even if I can’t find it, To move forward, to find proof is to move backward.


The result is obvious, we have found the first C page, then even if we go back, but in fact when I click, we go forward


2. Then let’s try Now delete the path after the first B page, then the saved path is: ['/a', '/b'],


Then when I click the back button, he will actually Entering the C page, we can see the following flow chart

At this time, if we click the back button here, we will go to the C page, but the saved path is `'/c' ` has been deleted by me, so the judgment is to move forward.

1. Would it be better if we filtered duplicate page paths? In fact, it is the same.


If we have 5 page paths, 2 duplicates are filtered out Yes, there are only 3 page paths.


Then I can’t find it when I return to the fourth path. Then the next two pages will be counted as forward.


So from the current point of view, the best way is to record every page, but make each page different


Then we can put it in the url Put a random

string

code implementation on it:


// 当没有key的时候会进入两次 beforeEach,我们只需保存带key的就行
const updateNavigations = (to) => {
 if (to.query[pathKey]) {
  store.commit('UPDATE_NAVIGATIONS', {path: to.fullPath})
 }
}

router.beforeEach((to, from, next) => {
 let toIndex = store.state.navigations.findIndex(path => path === to.fullPath)
 if (toIndex >= 0) { // 存在该路径
  let len = store.state.navigations.length-1
  if (toIndex === len) { // 当前路径是最后一条,证明是同一个页面
   console.log('refresh') 
  } else { // 后退
   store.commit('UPDATE_ROUTER_DIRECTION', { routerDirection: 'back' }) // 后退标志
   store.commit('DELETE_NAVIGATION', { index: toIndex }) // 删除当前路径后面的路径
  }
 }else{ // 不存在该路径
  store.commit('UPDATE_ROUTER_DIRECTION', { routerDirection: 'forward' }) // 前进标志
  updateNavigations(to) // 保存该连接
 }

 const query = { ...to.query }
 // 存在就直接next, 防止死循环
 if (!query['APP_KEY']) { // 不存在添加key ,再次 next
  query['APP_KEY'] = Math.random().toString(16).substring(2)
  next({ path: to.path, query})
 }else{
  next()
 }
})
Copy after login


We will use the above code You can add a random string of APP_KEY to the URL, so that even if the same page is in the path we save, it will actually be different. The logic can be executed normally


The above basically solves the problem of the browser navigation bar

2. Sliding switching on IOS

On the IOS web page, you can slide left and right to switch, even if you don't do a transition animation.


A problem will arise at this time.

Still ABC page

A -> B -> C

When we reach the C page and then slide to the left, finish sliding him Just enter the B page, but at this time it will still enter our beforeEach hook function and execute our above logic.


That will trigger our transition animation. You will find that two switches are performed.

So I found a method on the Internet to fix ios and slide left to execute the animation again#2259

The code is like this


let touchEndTime = Date.now()

window.addEventListener('touchend', () => {
 touchEndTime = Date.now()
})

router.beforeEach((to, from, next) => {
 if ((Date.now() - touchEndTime) < 377) { // ios滑动切换
  store.commit(&#39;UPDATE_ROUTER_DIRECTION&#39;, { routerDirection: &#39;&#39; })
 }
})
Copy after login


The above is also easy to understand, that is, we get the moment when the finger finally leaves the screen, and then compare it in beforeEach.
The difference between the last moment when the finger leaves the screen and our own transition in beforeEach Less than 337, even if it is the sliding switching of IOS

, that will solve the sliding switching problem of IOS.

But when IOS slides right to switch, it cannot monitor the moment when the finger leaves the screen (I don’t know what the ghost is), so IOS slides right to switch and cannot be judged as above.

I haven’t found a solution to this. For the time being, I can only solve the switch of left swiping back in IOS.

Basically, the two most troublesome points are the above two points. The rest can be set by listening to events, which is not difficult

Online DEMO Demonstration

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of how to use jQuery class name selector (.class)

vue dynamic binding component child and parent components Detailed explanation of the steps to implement multi-form verification

#

The above is the detailed content of Vue mobile terminal routing switching case analysis. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

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)

Detailed explanation of Oracle error 3114: How to solve it quickly Detailed explanation of Oracle error 3114: How to solve it quickly Mar 08, 2024 pm 02:42 PM

Detailed explanation of Oracle error 3114: How to solve it quickly, specific code examples are needed. During the development and management of Oracle database, we often encounter various errors, among which error 3114 is a relatively common problem. Error 3114 usually indicates a problem with the database connection, which may be caused by network failure, database service stop, or incorrect connection string settings. This article will explain in detail the cause of error 3114 and how to quickly solve this problem, and attach the specific code

How to switch between 4g and 5g on Xiaomi Mi 14Ultra? How to switch between 4g and 5g on Xiaomi Mi 14Ultra? Feb 23, 2024 am 11:49 AM

Xiaomi 14Ultra is one of the most popular Xiaomi models this year. Xiaomi 14Ultra not only upgrades the processor and various configurations, but also brings many new functional applications to users. This can be seen from the sales of Xiaomi 14Ultra mobile phones. It is very popular, but there are some commonly used functions that you may not know yet. So how does Xiaomi 14Ultra switch between 4g and 5g? Let me introduce the specific content to you below! How to switch between 4g and 5g on Xiaomi 14Ultra? 1. Open the settings menu of your phone. 2. Find and select the "Network" and "Mobile Network" options in the settings menu. 3. In the mobile network settings, you will see the "Preferred network type" option. 4. Click or select this option and you will see

Operation tutorial for switching from win11 home version to professional version_Operation tutorial for switching from win11 home version to professional version Operation tutorial for switching from win11 home version to professional version_Operation tutorial for switching from win11 home version to professional version Mar 20, 2024 pm 01:58 PM

How to convert Win11 Home Edition to Win11 Professional Edition? In Win11 system, it is divided into Home Edition, Professional Edition, Enterprise Edition, etc., and most Win11 notebooks are pre-installed with Win11 Home Edition system. Today, the editor will show you the steps to switch from win11 home version to professional version! 1. First, right-click on this computer on the win11 desktop and properties. 2. Click Change Product Key or Upgrade Windows. 3. Then click Change Product Key after entering. 4. Enter the activation key: 8G7XN-V7YWC-W8RPC-V73KB-YWRDB and select Next. 5. Then it will prompt success, so you can upgrade win11 home version to win11 professional version.

Switch the dual system boot mode of Apple computer Switch the dual system boot mode of Apple computer Feb 19, 2024 pm 06:50 PM

How to switch between Apple dual systems when starting up Apple computers are powerful devices. In addition to their own macOS operating system, you can also choose to install other operating systems, such as Windows, to achieve dual system switching. So how do we switch between the two systems when booting? This article will introduce to you how to switch between dual systems on Apple computers. First of all, before installing dual systems, we need to confirm whether our Apple computer supports dual system switching. Generally speaking, Apple computers are based on

Analysis of the meaning and usage of midpoint in PHP Analysis of the meaning and usage of midpoint in PHP Mar 27, 2024 pm 08:57 PM

[Analysis of the meaning and usage of midpoint in PHP] In PHP, midpoint (.) is a commonly used operator used to connect two strings or properties or methods of objects. In this article, we’ll take a deep dive into the meaning and usage of midpoints in PHP, illustrating them with concrete code examples. 1. Connect string midpoint operator. The most common usage in PHP is to connect two strings. By placing . between two strings, you can splice them together to form a new string. $string1=&qu

How to use shortcut keys for switching workbooks in excel How to use shortcut keys for switching workbooks in excel Mar 20, 2024 pm 01:50 PM

In the application of excel software, we are accustomed to using shortcut keys to make some operations easier and faster. Sometimes there is related data between multiple tables in excel. When we view it, we have to constantly switch between tasks. If there is a faster switching method, it will save a lot of wasted time on switching, which will greatly help improve work efficiency. What method can be used to complete quick switching? To address this issue, the editor will talk about it today The content is: How to use the shortcut keys for switching workbooks in Excel. 1. First, you can see multiple workbooks at the bottom of the open excel table. You need to quickly switch between different workbooks, as shown in the figure below. 2. Then press the Ctrl key on the keyboard without moving, and select the job to the right if you need to

Parsing Wormhole NTT: an open framework for any Token Parsing Wormhole NTT: an open framework for any Token Mar 05, 2024 pm 12:46 PM

Wormhole is a leader in blockchain interoperability, focused on creating resilient, future-proof decentralized systems that prioritize ownership, control, and permissionless innovation. The foundation of this vision is a commitment to technical expertise, ethical principles, and community alignment to redefine the interoperability landscape with simplicity, clarity, and a broad suite of multi-chain solutions. With the rise of zero-knowledge proofs, scaling solutions, and feature-rich token standards, blockchains are becoming more powerful and interoperability is becoming increasingly important. In this innovative application environment, novel governance systems and practical capabilities bring unprecedented opportunities to assets across the network. Protocol builders are now grappling with how to operate in this emerging multi-chain

How to switch dual system settings on Huawei mobile phones How to switch dual system settings on Huawei mobile phones Feb 20, 2024 am 10:09 AM

With the rapid development of smartphones, Huawei, as a leading technology company, has launched many popular mobile phone products. Among them, Huawei dual system is a feature that makes many users excited. Through Huawei dual system, users can run two operating systems, such as Android and HarmonyOS, on the same mobile phone at the same time. This feature allows for greater flexibility and convenience. So, how to switch settings between Huawei dual systems? Let’s find out together. First, before switching to dual system setup on your Huawei phone,

See all articles