Home  >  Article  >  Web Front-end  >  An article introduces how to jump to the page in Uniapp

An article introduces how to jump to the page in Uniapp

PHPz
PHPzOriginal
2023-04-23 09:12:402665browse

In recent years, mobile application development has become a trend, and Uniapp, as a development framework suitable for mobile terminals, is widely welcomed by developers. In the development of Uniapp, jumping to the page is a very common operation. This article will introduce the methods and precautions for jumping the page in Uniapp.

1. Methods to jump to the page

In Uniapp, you can use the two methods uni.navigateTo and uni.redirectTo to jump to the page. The difference between the two is that the former will add the current page to the page. Stack, you can return to the previous page through the uni.navigateBack method; the latter will not retain the current page, and you cannot return to the previous page after jumping through uni.switchTab.

  1. uni.navigateTo method

Use the uni.navigateTo method to jump to the page. The sample code is as follows:

uni.navigateTo({
  url: '/pages/home/home',
  success: function(res) {
    console.log('跳转成功', res)
  },
  fail: function(err) {
    console.log('跳转失败', err)
  }
})

The url parameter indicates the page to jump to The page path can be jumped through an absolute path starting with / or a relative path starting with ./. For example, /pages/home/home is a page path. The success callback function represents the callback after a successful jump, and the fail represents the callback after a failed jump.

  1. uni.redirectTo method

Use the uni.redirectTo method to jump to the page. The sample code is as follows:

uni.redirectTo({
  url: '/pages/home/home',
  success: function(res) {
    console.log('跳转成功', res)
  },
  fail: function(err) {
    console.log('跳转失败', err)
  }
})

url, success and fail parameters are the same as uni The .navigateTo method is the same and will not be described again.

2. Notes

When jumping to a page, you need to pay attention to the following matters:

  1. The page path must be correct

When using uni.navigateTo or uni.redirectTo, you need to ensure that the path passed in the url parameter is correct, otherwise you will not be able to jump to the target page.

  1. It is recommended to use absolute paths for page paths

To avoid path errors, it is recommended to use absolute paths starting with / for page jumps instead of using relative paths starting with ./ path. At the same time, generally during the development process, we will extract the page path into the configuration file for subsequent modification and maintenance. At this time, absolute paths should also be used.

  1. Do not jump more than 10 times in a row

When jumping to a page, it is not recommended to jump multiple times in a row, especially to a page other than the current page. , because the page stack may not be managed properly, it is best not to exceed 10 jumps.

  1. Don’t abuse uni.navigateBack

When doing page stack management, you should not abuse the uni.navigateBack method, because if there are too many pages to jump, it will If the application is stuck, attention should be paid to controlling the number and frequency of page jumps.

Through this article, we have learned about the methods and precautions for Uniapp jump page, and hope it will be helpful to everyone in Uniapp development.

The above is the detailed content of An article introduces how to jump to the page in Uniapp. 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