Home > Article > Web Front-end > What should I do if the pop-up window does not pop up when uniapp obtains information?
With the rapid development of mobile Internet, more and more applications and websites are developed using uniapp technology. In the development of uniapp, a problem often encountered is that the window does not pop up when obtaining information. This article will introduce how to solve this problem in uniapp.
During the development process using uniapp, we often encounter scenarios where users need to be prompted, such as confirmation pop-ups, message notifications, etc. However, if you are not familiar with uniapp development, you may find that there is no pop-up window when obtaining information in uniapp, which is very distressing.
Specifically, when we call the method provided by uniapp to obtain data, such as the wx.getUserInfo()
method of the WeChat applet, a Promise object will be returned. In JavaScript, we can process the obtained data through the then() method of the Promise object, such as displaying it on the page or popping up a confirmation pop-up window. But in uniapp, if you directly use the uni.<Platform>.getUserInfo()
method to obtain user information, no confirmation pop-up window will pop up, and there will be no prompt.
To solve this problem, you first need to understand some basic knowledge of uniapp. In uniapp, we usually use encapsulated APIs to call native APIs. The implementation of the encapsulated API is usually by calling the native API, and then processing it according to the characteristics of different platforms, and finally returning the required results.
For example, when we use the wx.getUserInfo()
method in the WeChat applet to obtain user information, the WeChat applet will pop up a confirmation pop-up window to prompt the user whether Authorizes the app to obtain user information. If the user authorizes it, the WeChat applet will return user information to the developer program; if the user refuses authorization, the WeChat applet will return an error message.
However, in uniapp, due to the need to be compatible with multiple platforms, when encapsulating the API, you cannot directly call the native API to obtain user information. On the contrary, the uni.<Platform>.getUserInfo()
method encapsulated by uniapp only returns the user information to the developer program without popping up a confirmation pop-up window or other prompts.
Now that the cause is clear, the solution is also obvious. In uniapp, if you need to obtain user information and want to have a confirmation pop-up window or other prompts, you need to use the API provided by uniapp.
Specifically, you can use uniapp’s message prompt box API uni.showModal()
, and weak prompt box API uni.showToast()
to achieve this. Prompt function. An example is as follows:
uni.showModal({ title: '提示', content: '是否允许获取用户信息?', success: function (res) { if (res.confirm) { uni.<平台>.getUserInfo({ success: function (res) { console.log(res.userInfo); } }); } } });
In the above code, before we call the uni.<Platform>.getUserInfo() method, a confirmation pop-up window will pop up to prompt the user whether to allow access to user information. If the user clicks the confirmation button, the uni.<Platform>.getUserInfo() method will be executed and the obtained user information will be printed.
In addition to the above two APIs, uniapp also provides many other APIs to help us implement functions such as prompts. You need to choose and use it according to the specific situation.
In general, in uniapp development, if you want to obtain user information and need a prompt function, you cannot use the native API directly, but need to use the API provided by uniapp. The implementation of the encapsulated API is usually by calling the native API, and then processing it according to the characteristics of different platforms, and finally returning the required results. Among them, methods such as the message prompt box API uni.showModal()
and the weak prompt box API uni.showToast()
provided by uniapp are often used to implement the prompt function.
The above is the detailed content of What should I do if the pop-up window does not pop up when uniapp obtains information?. For more information, please follow other related articles on the PHP Chinese website!