g-watcher

1.0.6 • Public • Published

g-watcher

非常轻量级的集成js全局状态侦听管理及自定义事件发布订阅的工具库

Function

init-初始化方法

args

args 说明 类型 默认值
obj 需要侦听的对象 object undefined

return

Watcher的class实例

import watcher from 'g-watcher'
 // 初始化一个watcher实例,如下,watcher中的width和height属性将会被监听
const watcher = watcher.init({ width: 12, height: 24 })

$watch-注册侦听(实例方法)

args

args 说明 类型 默认值
property 需要侦听的对象中的属性 any undefined
callback 回调函数,回调中的参数第一个为新值,第二个为旧值 function undefined

return

void

// 这里注册一个侦听
watcher.$watch('width', fun)
// 这里修改width
watcher.width = 24

$offWatch-取消侦听(实例方法)

args

args 说明 类型 默认值
property 需要取消侦听的对象中的属性 any undefined
callback 回调函数,回调中的参数第一个为新值,第二个为旧值 function undefined

return

void

// 这里取消一个侦听
watcher.$offWatch('width', fun)

$on-订阅(实例方法)

args

args 说明 类型 默认值
type 自定义订阅事件类型 string undefined
callback 回调函数,回调中的参数第一个为新值,第二个为旧值 function undefined

return

void

// 这里订阅一个事件
watcher.$on('myfun', (params) => {
   // 触发回调
    console.log(oldValue, newValue)
})

$emit-发布(实例方法)

args

args 说明 类型 默认值
type 自定义订阅事件类型 string undefined
params 参数 any undefined

return

void

// 这里发布一个事件,那么订阅的回调函数将会被执行
watcher.$emit('myfun', 123)

$off-注销订阅(实例方法)

args

args 说明 类型 默认值
type 自定义订阅事件类型 string undefined

return

void

// 注销一个订阅事件
watcher.$off('myfun')

Readme

Keywords

none

Package Sidebar

Install

npm i g-watcher

Weekly Downloads

10

Version

1.0.6

License

none

Unpacked Size

250 kB

Total Files

7

Last publish

Collaborators

  • gxb1