-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
发布订阅模式 #47
Comments
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
emit和on
这一点有点像
vuex
和redux
里面的某部分概念,也跟$emit
和$on
和node自带的event
模块作用很相像,其实可以这样理解,如果单独用对象把数据存起来,数据改变的时候没有人会追踪到,所以这里在每次改变数据前都放入一个或多个回调函数形成队列去监听on方法
,这些回调函数在队列中等待,直到触发了某些机制,这些函数才按顺序逐一回来触发emit方法
,从而可以在这个时刻监听到新的数据变化并完成逻辑简单的队列
可以没有仓库存放中介值,也可以
$emit
和$on
的健值对,只有一个简单的队列,按放入的顺序,然后按顺序重新执行出来在上面的基础上添加健值对,可以改写成下面这样,这样写可以应付大部分发布订阅模式的情况
类似vuex和redux
继续往下改造,可以改成类似
vuex
的模式,在仓库中留着一份共享的数据,然后利用on
缓存回调函数事件放入队列里面,然后利用emit
在合适的时候释放该队列组件通信
发布订阅模式可以用在,两个组件之间的通信
实现双向数据绑定
配合
Object.defineProperty
实现类似Vue的双向数据绑定ES6的写法
The text was updated successfully, but these errors were encountered: