Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get parameters by scanning QR code in Mini Program

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shares with you how to obtain parameters by scanning QR codes in Mini programs. Xiaobian thinks it is quite practical, so share it with you to learn. I hope you can gain something after reading this article. Let's not say much. Let's have a look together with Xiaobian.

First of all, configure it on the background control page of Mini programs, as shown in the figure:

Click the Add button in the upper right corner of the image above to configure:

Four items need to be filled in. The prefix rule is not occupied by default. When we convert the ordinary link into a QR code through the QR code tool or qcodejs, we call the WeChat scan function to scan the QR code, and WeChat will jump to the specific page of the target Mini programs.

But sometimes we need to pass some parameters, through the above ordinary link to pass, the way to pass is to add parameters after the ordinary link, parameter format is query string, such as http://www.a.com? name=zs&age=18, so how do you get this data inside Mini programs? Observe the following code:

onLoad (option) { console.log(option) }

Generally, we will print the default parameter option of this function in the onload function of the life cycle function of Mini programs. Option is the parameter passed from the previous page. However, it should be noted here that this is the way to pass parameters between internal pages of Mini programs. It is not applicable to scanning 2D above to obtain parameters in 2D code. Many students are easy to confuse the method of obtaining parameters in 2D code with the method of obtaining values between pages.

These two methods pass parameters in the same way, both of which append question marks and query strings after the link, but the way to obtain them is different.

Scanning the QR code into Mini programs does not cause jumps between pages, so the parameters passed in the link cannot be obtained directly from option.

So how do we get it? View official documents:

The default parameter option of onload event has a q attribute, which needs to be decoded once with decodeURIComponent to obtain the complete content of the original QR code, for example: "http://www.a.com? name=zs&age=18"。After getting this complete link, we extract the parameters again. The main process is to process the string.

import qs from "qs"onLoad (option) { console.log(option) if (option.q) { let url = decodeURIComponent(option.q) // url http://www.a.com? name=zs&age=18; let paramstr = url.split("/\?/ ")[1]; // name=zs&age=18; let params = qs.parser(paramstr); //The final result is: params {name:'zs', age:18} }} The above is how to obtain parameters by scanning QR codes in Mini programs. Xiaobian believes that some knowledge points may be seen or used in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report