Skip to content

Commit

Permalink
'eventMixin'
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoAlone committed Apr 19, 2021
1 parent 0f77da4 commit 5ee5677
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion source-code/src/notes/整体流程/4.eventsMixin
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ Vue.prototype.$emit = function (event: string): Component {
}
```
代码分析:
1.传入触发事件名event,以及处理器所需参数args
2.invokeWithErrorHandling实际就是用try/catch包装了一下监听器,当报错时用handleError方法对报错进行处理


### $once
$once本质是在触发了一次事件监听器后,移除改监听器
```js
Vue.prototype.$once = function (event: string, fn: Function): Component {
const vm: Component = this
function on () {
vm.$off(event, on)
fn.apply(vm, arguments)
}
on.fn = fn
vm.$on(event, on)
return vm
}
```
代码分析:
1. 封装一个监听回调函数,内容包括,移除该监听器,执行监听函数,并将该新函数与fn进行关联
> $off中的cb.fn === fn就是为了判断该监听器是否是$once中注册的

0 comments on commit 5ee5677

Please sign in to comment.