Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tryOnMounted): support target arguement (#3185)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
Showing
5 changed files
with
36 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import { getCurrentInstance, onBeforeUnmount } from 'vue-demi' | ||
import type { Fn } from '../utils' | ||
import { onBeforeUnmount } from 'vue-demi' | ||
import { type Fn, getLifeCycleTarget } from '../utils' | ||
|
||
/** | ||
* Call onBeforeUnmount() if it's inside a component lifecycle, if not, do nothing | ||
* | ||
* @param fn | ||
* @param target | ||
*/ | ||
export function tryOnBeforeUnmount(fn: Fn) { | ||
if (getCurrentInstance()) | ||
onBeforeUnmount(fn) | ||
export function tryOnBeforeUnmount(fn: Fn, target?: any) { | ||
const instance = getLifeCycleTarget(target) | ||
if (instance) | ||
onBeforeUnmount(fn, instance) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
// eslint-disable-next-line no-restricted-imports | ||
import { getCurrentInstance, onUnmounted } from 'vue-demi' | ||
import type { Fn } from '../utils' | ||
import { onUnmounted } from 'vue-demi' | ||
import { type Fn, getLifeCycleTarget } from '../utils' | ||
|
||
/** | ||
* Call onUnmounted() if it's inside a component lifecycle, if not, do nothing | ||
* | ||
* @param fn | ||
* @param target | ||
*/ | ||
export function tryOnUnmounted(fn: Fn) { | ||
if (getCurrentInstance()) | ||
onUnmounted(fn) | ||
export function tryOnUnmounted(fn: Fn, target?: any) { | ||
const instance = getLifeCycleTarget(target) | ||
if (instance) | ||
onUnmounted(fn, instance) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters