Home > WeChat Applet > Mini Program Development > Detailed analysis of variables and scope of WeChat applet

Detailed analysis of variables and scope of WeChat applet

不言
Release: 2018-08-16 17:23:02
Original
4182 people have browsed it

This article brings you a detailed analysis of the variables and scope of the WeChat applet. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Global variables

The variables and methods in app.js are global.

//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: {
    userInfo: null,
    basePath: 'http://127.0.0.1:8086'
  }
  
})
Copy after login

On other pages, you can get the methods and variables inside through getApp(). After the console comes out:

If you want to get it, we will get it in advance To set the basePath, we can do this:

var app=getApp();

var basePath = app.globalData.basePath;
Copy after login

The same is true for obtaining the user's login information or other methods;

Second, partial page Data interaction within

All data on the local page originates from the data object within js:

On the page, you can directly use the data within Data;

Assignment:

  areaChange:function(e){
    //这里获取到了数组角标
    console.log(e.detail.value)
    var index1 = e.detail.value
    this.setData({
      index: index1
    })
    // console.log("地区:" + this.data.areaListArray[index1])
    console.log(this.data.areaList[index1])
    console.log(this.data.areaIdList[index1])
  }
Copy after login

Within the method, you can directly use this.setData() to define variables and assign values. Only variables defined in this way can be used on the entire page. For internal use.

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);
      }
    })
  
  },
Copy after login

If this points to an unknown variable, you can assign this to other variables before using it.

In this example, this is assigned to that and then used.

Related recommendations:

WeChat Mini Program Example: How to introduce external js files (graphics and text)

WeChat Mini Program Example: Introducing the code implementation of the framework WeUI

The above is the detailed content of Detailed analysis of variables and scope of WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template