Code Monkey home page Code Monkey logo

miniapp-hook's Introduction

微信小程序 Hook

hook 小程序组件(App, Page, Component)的生命周期方法。

Usage

开启微信小程序的 npm 支持: https://developers.weixin.qq.com/miniprogram/dev/devtools/npm.html

package.json

{
  "dependencies": {
    "miniapp-hook": "0.1.1"
  }
}
    npm install

配置钩子

由于 App,Page等方法是框架提供的,因此最后必要直接全局的 App,Page 等方式,建议使用包装的方式创建新的函数。一个示例如下:

app.new.js

const { _App, _Page, _Component, appLogger, pageLogger} = require('miniapp-hook');

_App.use(appLogger);
_Page.use(pageLogger);
_Page.use({
    onLoad() {
        return {
            before() {
                console.log(this.route, 'new Page.onLoad...... before', this.oldName);
            }
        };
    }
});
_Component.use({
    'methods.onTap': function () {
      return {
          before(e) {
              wx.showModal({
                  content: 'newComponent.onTap'
              });
          }
      }
    }
});

app.js

import {_App as App} from './app.new';
App({
    onLaunch () {
    }
})

如果你的小程序原本就已经包装了 App, Page 等框架方法,那么也可以再包装直接使用,已 App 为例:

app.new.js

import OldApp from './App.old';
const { _App } = require('miniapp-hook');
const newApp = _App.create(OldPage); // OldApp 是你自定义的App包装函数
newApp.use({
    onLaunch() {
        return {
            before() {
                console.log('new App.onLaunch...... before 1', this.oldName);
            }
        };
    }
})
export default newApp;

app.js

import {_App as App} from './app.new';
App({
    onLaunch () {
    }
})

pages/sample/index.js

import {_Page as Page} from './app.new';
Page({
    onLoad () {
    }
})

miniapp-hook's People

Contributors

xesam 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.