Skip to content

Commit

Permalink
fix: fix tryOnMounted in vue2 (#3658)
Browse files Browse the repository at this point in the history
  • Loading branch information
Doctor-wu committed Dec 27, 2023
1 parent dd82044 commit ce420c4
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/shared/tryOnBeforeMount/index.ts
Expand Up @@ -11,7 +11,7 @@ import { type Fn, getLifeCycleTarget } from '../utils'
export function tryOnBeforeMount(fn: Fn, sync = true, target?: any) {
const instance = getLifeCycleTarget(target)
if (instance)
onBeforeMount(fn, instance)
onBeforeMount(fn, target)
else if (sync)
fn()
else
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/tryOnBeforeUnmount/index.ts
Expand Up @@ -10,5 +10,5 @@ import { type Fn, getLifeCycleTarget } from '../utils'
export function tryOnBeforeUnmount(fn: Fn, target?: any) {
const instance = getLifeCycleTarget(target)
if (instance)
onBeforeUnmount(fn, instance)
onBeforeUnmount(fn, target)
}
4 changes: 2 additions & 2 deletions packages/shared/tryOnMounted/index.ts
Expand Up @@ -10,9 +10,9 @@ import { type Fn, getLifeCycleTarget } from '../utils'
* @param target
*/
export function tryOnMounted(fn: Fn, sync = true, target?: any) {
const instance = getLifeCycleTarget(target)
const instance = getLifeCycleTarget()
if (instance)
onMounted(fn, instance)
onMounted(fn, target)
else if (sync)
fn()
else
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/tryOnUnmounted/index.ts
Expand Up @@ -11,5 +11,5 @@ import { type Fn, getLifeCycleTarget } from '../utils'
export function tryOnUnmounted(fn: Fn, target?: any) {
const instance = getLifeCycleTarget(target)
if (instance)
onUnmounted(fn, instance)
onUnmounted(fn, target)
}
6 changes: 2 additions & 4 deletions packages/shared/utils/index.ts
@@ -1,4 +1,4 @@
import { getCurrentInstance, isVue3 } from 'vue-demi'
import { getCurrentInstance } from 'vue-demi'

export * from './is'
export * from './filters'
Expand Down Expand Up @@ -117,7 +117,5 @@ export function objectEntries<T extends object>(obj: T) {
}

export function getLifeCycleTarget(target?: any) {
const instance = target || getCurrentInstance()

return isVue3 ? instance : instance?.proxy
return target || getCurrentInstance()
}

0 comments on commit ce420c4

Please sign in to comment.