wxa-plugin-bind-hijack

NPM versionhijack 事件劫持 - 图2

劫持小程序bind事件,目前暂仅支持拦截tap

install

  1. npm install -S @wxa/plugin-bind-hijack

Usage

import plugin

  1. const BindCapturePlugin = require('@wxa/plugin-bind-capture');
  2. new BindCapturePlugin([
  3. 'tap', // 默认值
  4. 'getuserinfo',
  5. ]),

add wxaTapCapture functionn

  1. /**
  2. * wxa plugin
  3. */
  4. import $$log from '@/services/log';
  5. export default ()=>(vm, type)=>{
  6. if (['Page', 'Component'].indexOf(type) == -1) return;
  7. /**
  8. * 拦截函数命名为: wxaHijack + 事件名,驼峰
  9. * 即:`wxaHijack${event[0].toUpperCase()}${event.substr(1)}`;
  10. */
  11. vm.wxaHijackTap = function(e){
  12. // do sth, ie: log
  13. $$log('tap event', e);
  14. // execute origin funtion
  15. let tap = e.currentTarget.dataset.tap || e.target.dataset.tap;
  16. if(tap && this[tap]){
  17. this[tap].bind(this)(e);
  18. }else{
  19. console.log(`${tap}方法不存在`);
  20. }
  21. }
  22. wm.wxaHijackGetuserinfo = function(e){
  23. console.log('getuserinfo fire');
  24. }
  25. }