Home >Web Front-end >uni-app >uniapp jump page passes a large number of parameters

uniapp jump page passes a large number of parameters

WBOY
WBOYOriginal
2023-05-21 22:04:062718browse

With the development of the mobile Internet, small programs and H5 applications are gradually emerging in various scenarios. As a development framework that can adapt to multiple platforms at the same time, uniapp has great advantages in cross-terminal development. However, if uniapp needs to pass a large number of parameters when jumping to the page, it may encounter some problems. The following will start from the actual needs and introduce the solution for passing a large number of parameters on the uniapp jump page.

  1. Problem Background

In actual projects, we may encounter a scenario where when jumping from one page to another, we need to pass a large number of Parameters, for example, an order details page needs to pass the order number, order status, order time, recipient information, product information, etc. If you use the jump method that comes with uniapp, that is, pass parameters in APIs such as navigateTo, you need to combine all the parameters that need to be passed into an object before jumping, and then pass the object as a parameter to API. This method is suitable for cases with relatively few parameters, but when there are too many parameters, the object will become very large and may exceed the API parameter length limit.

  1. Solution

In response to the above problems, we can use a more flexible method of passing parameters, that is, carrying parameters in the routing address. This method is used in uniapp The implementation method is as follows:

(1) When jumping, concatenate all the parameters that need to be passed into a string, and then append it as a parameter to the routing address of the target page.

// 例如目标页面地址为'/pages/order/detail', 我们需要传递的参数如下
var orderId = '123456';
var orderStatus = '已发货';
var orderTime = '2021-01-01';
// 将这些参数拼接在路由地址后面,形成新的跳转地址
var targetUrl = '/pages/order/detail?orderId=' + orderId + '&orderStatus=' + orderStatus + '&orderTime=' + orderTime;
// 跳转到目标页面
uni.navigateTo({
   url: targetUrl
});

(2) In the target page, use the uni.getQueryParam method to obtain the parameters carried in the jump address.

// 在目标页面中获取跳转地址中携带的参数
var orderId = uni.getQueryParam('orderId');
var orderStatus = uni.getQueryParam('orderStatus');
var orderTime = uni.getQueryParam('orderTime');

It should be noted that this method can only obtain the parameters carried in the routing address, but cannot obtain the parameters passed through APIs such as navigateTo.

  1. Practical Suggestions

Using the routing address to carry parameters can effectively avoid the problem of too large parameter objects and exceeding the parameter length limit when passing too many parameters. But in practice, we also need to pay attention to the following suggestions:

(1) Parameter combinations should be standardized

When splicing parameter strings, we should try to follow certain specifications, such as parameter names and parameters Values ​​are connected with equal signs, and different parameters are connected with &. This facilitates subsequent parsing of parameters in the target page and reduces the possibility of errors.

(2) The number of parameters must be controlled

Although using routing addresses to carry parameters can effectively avoid problems caused by too many parameters, we still need to control the number of parameters, especially in small programs In platforms with small memory and weak performance such as H5 and H5, unnecessary parameter transfer should be minimized to improve application performance and stability.

(3) Parameter data type should be considered

When using routing address to carry parameters, all parameters need to be converted into string form, so after obtaining parameters in the target page, some need to use Data type conversion is required where numbers, Boolean and other data types are used.

In short, in uniapp development, the problem of transferring a large number of parameters in the jump page can be solved by splicing routing addresses, but developers still need to pay attention to the specification, number and data type of the parameters passed. .

The above is the detailed content of uniapp jump page passes a large number of parameters. 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
Previous article:What ui does uniapp use?Next article:What ui does uniapp use?