Home > front end >  onScopeDispose vs scope.cleanups
onScopeDispose vs scope.cleanups

Time:05-30

I was checking out some source code to make sure my event bus was properly cleaned up when components were destroyed and ran in to scope.cleanups (from https://github.com/vueuse/vueuse/blob/main/packages/core/useEventBus/index.ts):

const scope = getCurrentScope()
scope?.cleanups?.push(/* handler */)

Is this an undocumented alternative to onScopeDispose (https://vuejs.org/api/reactivity-advanced.html#onscopedispose)? I can't seem to find any info about it.

CodePudding user response:

scope?.cleanups?.push() is almost the same implementation in onScopeDispose():

export function getCurrentScope() {
  return activeEffectScope
}

export function onScopeDispose(fn: () => void) {
  if (activeEffectScope) {
    activeEffectScope.cleanups.push(fn)            
  • Related