# wx-watch

> 利用watch在小程序中实现全局状态共享

Latest version **0.0.6** (published 2019-06-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install wx-watch
pnpm add wx-watch
yarn add wx-watch
bun add wx-watch
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2019-06-13 |
| First published | 2019-06-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | 马鹏达 |
| Maintainers | ma125120 |

## Links

- npm: https://www.npmjs.com/package/wx-watch
- Repository: https://github.com/ma125120/wx-watch
- Homepage: https://github.com/ma125120/wx-watch#readme
- Issues: https://github.com/ma125120/wx-watch/issues
- npm.io page: https://npm.io/package/wx-watch

## Recent versions

- 0.0.6 (latest) — 2019-06-13
- 0.0.5 — 2019-06-05
- 0.0.4 — 2019-06-03
- 0.0.3 — 2019-06-02
- 0.0.2 — 2019-06-02

## README

## 安装

```javascript
npm i wx-watch -S --production
```

## 使用
```javascript
// 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)
```

---
_Source: https://npm.io/package/wx-watch · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
