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 use WeChat Mini Programs api in JS

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use WeChat Mini Programs api in JS". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how to use WeChat Mini Programs api in JS.

The reasons for promise

WeChat Mini Programs's api uses object parameter callback mode, which can easily cause callback hell and make the code difficult to read, judge, modify and debug.

WeChat Mini Programs api example

/ / obtain user information wx.getSetting ({success: res = > {if (res.authSetting ['scope.userInfo']) {/ / has been authorized. You can call getUserInfo directly to get avatar nickname without popping up wx.getUserInfo ({success: res = > {/ / you can send res to the backend to decode unionId this.globalData.userInfo = res.userInfo})

When you can see the two layers, the code is very awkward.

Promise Mini Program

Write a common function that can promise Mini Program api

Function promisify (method, options = {}) {return new Promise ((resolve, reject) = > {/ / assign the options object to the method called below options.success = resolve options.fail = err = > {reject (err)} wx [method] (options)})}

Use the example

The extra parameters passed are passed through object deconstruction

Promisify ('getUserInfo'). Then ((res) = > console.log (res)) .catch ((err) = > {console.error (err)}) promisify (' navigateTo', {url}) so far, I believe you have a better understanding of "how to use navigateTo', api in JS". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report