Home Web Front-end uni-app How does uniapp return after jumping without refreshing?

How does uniapp return after jumping without refreshing?

May 26, 2023 am 09:20 AM

Preface

As a cross-platform development framework, Uniapp has been accepted and used by more and more developers. In Uniapp, page jump is a very common operation. After page jump, sometimes it is necessary to retain the status of the original page, so that the next time you return to this page, you do not need to reload the data or perform some complicated operations again. operation. So, how to achieve the effect of returning without refreshing after jumping in Uniapp? This article will introduce it to you in detail.

Uniapp page jump

To perform page jump in Uniapp, we usually use the uni.navigateTo or uni.redirectTo method. Their specific differences are as follows:

  1. uni.navigateTo

When using the uni.navigateTo method to jump to a page, a new page will be added to the top of the stack of the current page. This new page will cover part of the current page, as follows As shown in the picture:

As you can see, we use uni.navigateTo in page A to jump to page B, and add a new content to page B. This The content will appear on top of page A, and when we return to page A, page B will be destroyed. The whole process is like a stack structure.

  1. uni.redirectTo

The difference from uni.navigateTo is that when using uni.redirectTo to jump to a page, the current page will be deleted and then jumped to A new page, as shown below:

As you can see, we used uni.redirectTo in page A to jump to page B, and added in page B A new content, but when we return to page A, page B has been destroyed, and the whole process is like a queue.

How to achieve the effect of returning without refreshing after jumping

Through the above introduction, we can know that when we need the effect of returning without refreshing after jumping, we cannot directly use uni.navigateTo or uni .redirectTo method, because both methods will destroy the page before the jump. So, how to achieve this?

Implementation ideas:

Go to the specified page through the uni.switchTab or uni.reLaunch method. These two methods have one feature, that is, no matter how you jump, the page will be refreshed, so pay attention You cannot use uni.navigateTo or uni.redirectTo methods here.

Add a tabBar tab to the page that needs to be jumped. The routing address of this tab must be the same as the routing address of the page that uni.switchTab or uni.reLaunch goes to. In this way, when we click this tab , it will jump to the specified page and retain the page status before the jump.

Implementation steps:

  1. Add the tabBar tab in the manifest.json file
"tabBar": {
  "color": "#999",
  "selectedColor": "#007AFF",
  "borderStyle": "black",
  "backgroundColor": "#FFFFFF",
  "list": [
    {
      "pagePath": "pages/index/index",
      "text": "首页",
      "iconPath": "static/img/tabBar/home.png",
      "selectedIconPath": "static/img/tabBar/home-selected.png"
    },
    {
      "pagePath": "pages/mine/mine",
      "text": "我的",
      "iconPath": "static/img/tabBar/mine.png",
      "selectedIconPath": "static/img/tabBar/mine-selected.png"
    }
  ]
},
Copy after login

Add the tabBar tab in the manifest.json file, It contains two pages, namely the home page and my page.

  1. Add a button to the page before jumping and jump to the specified page when clicked
<template>
  <view class='container'>
    <view class='content'>
      <button class='button' @click='jumpToMine'>跳转到我的页面</button>
    </view>
  </view>
</template>

<script>
  export default {
    methods: {
      jumpToMine() {
        uni.switchTab({ //注意这里使用了switchTab方法
          url: '/pages/mine/mine'
        })
      }
    }
  }
</script>

<style>
  .container {
    width: 100%;
    height: 100%;
    background-color: #FFFFFF;
  }

  .content {
    margin-top: 100px;
    text-align: center;
  }

  .button {
    width: 200px;
    height: 50px;
    background-color: #007AFF;
    color: #FFFFFF;
    border: none;
    border-radius: 10px;
  }
</style>
Copy after login

By adding a button, use the uni.switchTab method to jump when clicked Go to my page. Note here that you cannot use the uni.navigateTo or uni.redirectTo method.

  1. Add a tabBar tab to my page
<template>
  <view class='container'>
    <view class='content'>
      我的页面
    </view>

    <view class='tabBar'>
      <view class='tabBarItem' @click='jumpToHome'>
        <text class='tabBarIcon'>首页</text>
      </view>

      <view class='tabBarItem tabBarItem-selected'>
        <text class='tabBarIcon'>我的</text>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    methods: {
      jumpToHome() {
        uni.switchTab({
          url: '/pages/index/index'
        })
      }
    }
  }
</script>

<style>
  .container {
    width: 100%;
    height: 100%;
    background-color: #FFFFFF;
  }

  .content {
    margin-top: 100px;
    text-align: center;
  }

  .tabBar {
    position: fixed;
    bottom: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    height: 50px;
    padding: 5px;
    background-color: #FFFFFF;
    border-top: solid 1px #DDDDDD;
  }

  .tabBarItem {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    font-size: 14px;
    color: #999;
  }

  .tabBarItem-selected {
    color: #007AFF;
  }

  .tabBarIcon {
    font-size: 14px;
  }
</style>
Copy after login

In my page, we added a tabBar tab, which contains two tabBarItem , respectively the home page and mine, in which this tab of mine is set to the selected state.

  1. Effect Demonstration

Here I will show you the effect:

https://img-blog.csdn.net/20190118135008629?watermark/2 /text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Jsb2dwYW5fY2xvdWQ=/

The above is the entire content of this article. Through studying this article, I believe you have mastered how to achieve the effect of returning without refreshing after jumping in Uniapp. I hope to be helpful.

The above is the detailed content of How does uniapp return after jumping without 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)

What are the different types of testing that you can perform in a UniApp application? What are the different types of testing that you can perform in a UniApp application? Mar 27, 2025 pm 04:59 PM

The article discusses various testing types for UniApp applications, including unit, integration, functional, UI/UX, performance, cross-platform, and security testing. It also covers ensuring cross-platform compatibility and recommends tools like Jes

What debugging tools are available for UniApp development? What debugging tools are available for UniApp development? Mar 27, 2025 pm 05:05 PM

The article discusses debugging tools and best practices for UniApp development, focusing on tools like HBuilderX, WeChat Developer Tools, and Chrome DevTools.

How can you reduce the size of your UniApp application package? How can you reduce the size of your UniApp application package? Mar 27, 2025 pm 04:45 PM

The article discusses strategies to reduce UniApp package size, focusing on code optimization, resource management, and techniques like code splitting and lazy loading.

How can you use lazy loading to improve performance? How can you use lazy loading to improve performance? Mar 27, 2025 pm 04:47 PM

Lazy loading defers non-critical resources to improve site performance, reducing load times and data usage. Key practices include prioritizing critical content and using efficient APIs.

How can you optimize images for web performance in UniApp? How can you optimize images for web performance in UniApp? Mar 27, 2025 pm 04:50 PM

The article discusses optimizing images in UniApp for better web performance through compression, responsive design, lazy loading, caching, and using WebP format.

What are some common patterns for managing complex data structures in UniApp? What are some common patterns for managing complex data structures in UniApp? Mar 25, 2025 pm 02:31 PM

The article discusses managing complex data structures in UniApp, focusing on patterns like Singleton, Observer, Factory, and State, and strategies for handling data state changes using Vuex and Vue 3 Composition API.

What are computed properties in UniApp? How are they used? What are computed properties in UniApp? How are they used? Mar 25, 2025 pm 02:23 PM

UniApp's computed properties, derived from Vue.js, enhance development by providing reactive, reusable, and optimized data handling. They automatically update when dependencies change, offering performance benefits and simplifying state management co

How does UniApp handle global configuration and styling? How does UniApp handle global configuration and styling? Mar 25, 2025 pm 02:20 PM

UniApp manages global configuration via manifest.json and styling through app.vue or app.scss, using uni.scss for variables and mixins. Best practices include using SCSS, modular styles, and responsive design.

See all articles