diff --git a/packages/runtime-core/src/apiWatch.ts b/packages/runtime-core/src/apiWatch.ts index b3c92239..da3df303 100644 --- a/packages/runtime-core/src/apiWatch.ts +++ b/packages/runtime-core/src/apiWatch.ts @@ -14,7 +14,8 @@ import { isFunction, isString, hasChanged, - NOOP + NOOP, + remove } from '@vue/shared' import { currentInstance, @@ -264,11 +265,7 @@ function doWatch( return () => { stop(runner) if (instance) { - const effects = instance.effects! - const index = effects.indexOf(runner) - if (index > -1) { - effects.splice(index, 1) - } + remove(instance.effects!, runner) } } } diff --git a/packages/runtime-core/src/components/KeepAlive.ts b/packages/runtime-core/src/components/KeepAlive.ts index d5750d5a..8aae9b4e 100644 --- a/packages/runtime-core/src/components/KeepAlive.ts +++ b/packages/runtime-core/src/components/KeepAlive.ts @@ -10,7 +10,7 @@ import { import { VNode, cloneVNode, isVNode, VNodeProps } from '../vnode' import { warn } from '../warning' import { onBeforeUnmount, injectHook, onUnmounted } from '../apiLifecycle' -import { isString, isArray, ShapeFlags } from '@vue/shared' +import { isString, isArray, ShapeFlags, remove } from '@vue/shared' import { watch } from '../apiWatch' import { SuspenseBoundary } from './Suspense' import { @@ -297,7 +297,6 @@ function injectToKeepAliveRoot( ) { injectHook(type, hook, keepAliveRoot, true /* prepend */) onUnmounted(() => { - const hooks = keepAliveRoot[type]! - hooks.splice(hooks.indexOf(hook), 1) + remove(keepAliveRoot[type]!, hook) }, target) } diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index b08300cd..d5c35614 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts @@ -36,6 +36,13 @@ export const extend = ( return a as any } +export const remove = (arr: T[], el: T) => { + const i = arr.indexOf(el) + if (i > -1) { + arr.splice(i, 1) + } +} + const hasOwnProperty = Object.prototype.hasOwnProperty export const hasOwn = ( val: object,