In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points about how variables and scopes are defined in WeChat Mini Programs. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.
One, global variable
Variables and methods in app.js are global.
/ / app.jsApp ({onLaunch: function () {/ / Show local storage capacity var logs = wx.getStorageSync ('logs') | | [] logs.unshift (Date.now ()) wx.setStorageSync (' logs', logs) / / Log in to wx.login ({success: res = > {/ / send res.code to the backend for openId, sessionKey UnionId}}) / / get user information wx.getSetting ({success: res = > {if (res.authSetting ['scope.userInfo']) {/ / authorized You can directly call getUserInfo to get the avatar nickname without popping up wx.getUserInfo ({success: res = > {/ / you can send res to the backend to decode unionId this.globalData.userInfo = res.userInfo / / because getUserInfo is a network request May return / / after Page.onLoad, so add callback here to prevent this situation if (this.userInfoReadyCallback) {this.userInfoReadyCallback (res)})}, globalData: {userInfo: null BasePath: 'http://127.0.0.1:8086'}})
On other pages, you can get the methods and variables through getApp (). After console comes out:
If we want to get the basePath we set in advance, we can do this:
Var app=getApp (); var basePath = app.globalData.basePath
The same is true of getting the user's login information or other methods
Second, data interaction in local pages
All the data on the local page is derived from the data object in js:
On the page, you can use the data in data directly
Assignment:
AreaChange:function (e) {/ / the array corner label console.log (e.detail.value) var index1 = e.detail.value this.setData ({index: index1}) / / console.log ("region:" + this.data.areaListArray [index1]) console.log (this.data.areaList [index1]) console.log (this.data.areaIdList [index1])} is obtained here
Within the method, you can directly use this.setData () to define variables and assign values, and only variables defined in this way can be used throughout the page.
OnLoad: function (options) {var that = this; var app=getApp (); console.log (app); var basePath = app.globalData.basePath; wx.request ({method: "GET", url: basePath +'/ area/getAreaByLevel?level=1', success: function (res) {console.log (res); var areaListArray = []; var areaPkIdArray = [] For (var index in res.data.data) {areaListArray.push (res.data.data [index] .area) areaPkIdArray.push (res.data.data [index] .pkId)} that.setData ({/ / projectList: res.data.data.data, / / fileUrl: res.data.data.fileSystem areaList: areaListArray AreaIdList: areaPkIdArray})}, fail: function (res) {console.log (res) })}
If there is a this pointing to an unknown one, you can assign the this to another variable before using it.
In this case, this is assigned to that and then used.
These are all the contents of the article "how to define variables and scopes in WeChat Mini Programs". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.