search
HomeWeChat AppletMini Program DevelopmentSolution to disconnection and reloading during WeChat development

Analysis

微信小程序目前没有提供刷新API,所以要自己去记录当前操作,点击刷新重新执行一遍

Effect

Solution to disconnection and reloading during WeChat development

##Click 'Reload' to reload the page

Implementation

Since when working on a project, page rendering is inseparable from

Interface requests, so I encapsulated wx.request to determine whether the network is disconnected, and record this time after the network is disconnected Request

    /**
     * obj  request请求参数
     * cb   requrst请求成功回掉
     * page 当前page实例
    **/

    function wxRequest (obj, cb, page, type) {
        var isOne = true
        var cachFn = function () {
            wx.request({
                  url: obj.url,
                  data: obj.data || {},
                  method: obj.method || 'GET',
                  success: function (res) {
                    cb.call(page, res)
                    if (!page.data.isNet) {
                        page.setData({
                            isNet: true
                        })
                    }
                  },
                  // fail执行时当断网处理
                  fail: function () {
                      // 防止fail 有时会执行两次,影响渲染
                      if (!isOne) {
                          return
                      }
                    page.setData({
                        isNet: false,
                        isRequested: false
                    })
                    // 记录本次请求,加载时,执行page实例的reloadFn即可
                      page.reloadFn = wxRequest(obj, cb, page, 1)
                      isOne = false
                  }
            })
        }
    
        if (type) {
            page.isRequested = true
        }
    
        return type ? cachFn : cachFn()
    }

Application

 let data = {
      url: '',
      data: {},
      method: ''
    }
    wxTools.wxRequest(data, (res) => {
        // 数据渲染
        this.setData({})
    }, this)

When the network is disconnected, executing this.reloadFn will return to the last request, isNet and determine whether the network is disconnected

[Related recommendations]

1.

Complete source code of WeChat Mini Program

2.

WeChat Mini Program demo: Yangtao

The above is the detailed content of Solution to disconnection and reloading during WeChat development. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.