Giter Site home page Giter Site logo

wx-watch's Introduction

安装

npm i wx-watch -S --production

使用

// app.js
var { watchData, } = require('/miniprogram_npm/wx-watch/index.js');

App({
  onLaunch() {
    this.watchData(); /* 监听this.globalData的变化,并触发事件,其他页面监听的值必须在globalData中预先定义,否则无法监听 */
  },
  watchData,
  globalData: {
    userInfo: null,
  }
});

// 其他需要监听globalData的页面.js
var { getPage, FAIL } = require('../../miniprogram_npm/wx-watch/index.js');
const app = getApp();

// FAIL 为 一个失败的Symbol常量

/**
 * getPage(页面参数,app)
*/
getPage({
  watch: {
    userInfo(userInfo, oldUserInfo) {
      // 返回的数据存在三种情况
      // 1. 初始值,一般都是不存在的,可以使用 if (!userInfo) return ;直接中断
      // 2. 失败 (失败的时候可以通过赋值导出的常量FAIL)
      // 3. 成功

      if (!userInfo) return ;
      if (userInfo === FAIL) {
        // 失败
        this.setData({ isShowLogin: true });
      } else if (userInfo) {
        // 成功
        this.setData({ isShowLogin: false, userInfo: userInfo });
      }

      console.log(`来自app.glodalData的userInfo`);
    }
  },
  // 其他参数
}, app)

wx-watch's People

Contributors

ma125120 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.