In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What is the use of var that=this in WeChat Mini Programs's development? I believe many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
In WeChat Mini Programs's development, the declaration of var that = this is common. For example, the code is as follows!
Sample code 1
/ / index.js Page ({data: {toastHidden: true,}, loadData: function () {var that = this//) where that is declared Store this in that wx.request ({url: 'test.php', data: {a:' asides, b:'b'}, header: {'content-type':' application/json'}, success (res) {that.setData ({toastHidden: false}) / / that is used here, so you can get Page ({}) objects},})})
Line 9 declares var that = this; line 17 uses that.
If var that = this is not declared, and that is changed to this, the code is as follows!
Sample code 2
/ index.js Page ({data: {toastHidden: true,}, loadData: function () {wx.request ({url: 'test.php', data: {a:' a), b:'b'}, header: {'content-type':' application/json'}, success (res) {this.setData ({toastHidden: false})},})})
Running the code at this time will report the following error!
Learn from the error report that the attribute setData can not be read, why not read it? This is related to the scope of the this keyword!
This scope Analysis:
1. In Page ({}), the this keyword refers to the entire object Page ({}).
two。 Therefore, you can access or reset the variable data in Page ({}) through the this keyword.
3. However, the use of wx.request ({}) API in the loadData function makes it impossible to use this to get the Page ({}) object in wx.request ({}).
4. Although you can't use this to get a Page ({}) object in wx.request ({}), you can store this in a variable outside wx.request ({}), so you have the declaration var that = this. At this point, that refers to the whole object of Page ({}), so you can use that in wx.request ({}) to access or reset the variable data in Page ({}).
After reading the above, have you mastered the usage of var that=this in WeChat Mini Programs's development? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.