Wechat applet -- basic part (2) error prone

This article only introduces the error prone points of wechat applet and some commonly used js processing.

(1) Global variables

1. Define the global variable app.js

globalData: {
    imgSrc: 'https://yesmyart.cn/Public/img/common/',//Encapsulate the code to facilitate modification and improve efficiency

2. Call global variables

var app = getApp();//Define an app
  data: {
    imgSrc: app.globalData.imgSrc,//Call global variables
  onLoad: function (options) {
     //Global global data is called directly here
    var imgSrc=this.data.app.globalData.imgSrc;
     //Local data is called here. Compared with the former, it is more elegant and the result is the same
    var imgSrc=this.data.imgSrc

3. Assign or define a new value to the global variable

//Method 1: synchronous call,
app.globalData.imgSrc = openid;//The openid here is a variable. After the value is assigned successfully, it will become as follows: it is only a dynamic change. When the current code fragment is executed, it is not permanent
globalData: {
    imgSrc: openid,

//Method 2: asynchronous call
    imgSrc: openid,
})//To achieve the above effect, I suggest using asynchronous call. Wechat applets support asynchronous call. The former is prone to some bug s or writing changes
globalData: {
    imgSrc: openid,

(2) Jump after timer three seconds

//Timer login identification
      var indexInt=setInterval(function () {
        var userid = wx.getStorageSync("userid")
        if (userid == "" || userid == undefined) { //Here is the timer to determine whether the user is logged in. If not, continue to monitor. Pop up the window to prompt if necessary         
        else { 
          //Clear timer
            url: '../index/index_decorate/index_decorate'
          })//The switchTab is because the applet tabbar cannot jump directly after it is selected. You need to use this method to jump
      }, 3000)//Set jump time
//Timer 3 seconds jump
var indexInt=setInterval(function () {
          wx.switchTab({//Other jump methods wx.navigateTo and wx.redirectTo
            url: '../index/index_decorate/index_decorate'
          })//The switchTab is because the applet tabbar cannot jump directly after it is selected. You need to use this method to jump

      }, 3000)//Set jump time

(3) Conversion of string object

1.string string string to object
    var str1 = list.label;
    console.log(typeof str1)
    list.label = str1.split(",");
    console.log(typeof list.label)
2.string string to json object
var str=character string
var json_con = JSON.parse(str)//Can be successfully converted to json object

(4) The first three modifications of string or array truncation

     var arr = list.label// list.labelIs an array collection
     list.label = arr.slice(0, 3)//0First start,3From the first to the thirdlabel[0],label[1],label[2]

(5) Dynamically change the current title

// Change the current title
  wx.setNavigationBarTitle({ title: res.data.info.name })//title: title Content

(6) Share link returns the corresponding path (for example, the details page)

 onShareAppMessage: function (res) {
    if (res.from === 'button') {
    return {
      title: 'I'm the title',
      path: 'pages/index/index_decorate_detail/index_decorate_detail?id=' + this.data.id+'&idx='+idx,//The ID here is the detail page ID. in this page, js needs to have the corresponding onload(options) receiving function id=options.id to return to the sharing page when the user shares and clicks. The IDX here is analog multi-function transmission, and the receiving method is still options.idx

(7) Date interception

  // Top 4 of cut-off date
     if (list.created_time) {//Judge whether the date exists. If the date must exist, it can be identified without this method
         var str = list.created_time;//List. Created "time is 20170412
         list.created_time = str.substr(0, 4);//The result is 2017

Tags: JSON Fragment

Posted on Mon, 04 May 2020 15:06:38 -0400 by montyauto