Skip to content

Commit

Permalink
feat(tryOnBeforeUnmount): new function (vitest-dev#709)
Browse files Browse the repository at this point in the history
  • Loading branch information
xizher committed Sep 2, 2021
1 parent 69c38d6 commit f573b95
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/shared/index.ts
Expand Up @@ -21,6 +21,7 @@ export * from './syncRef'
export * from './throttledWatch'
export * from './toReactive'
export * from './toRefs'
export * from './tryOnBeforeUnmount'
export * from './tryOnMounted'
export * from './tryOnScopeDispose'
export * from './tryOnUnmounted'
Expand Down
37 changes: 37 additions & 0 deletions packages/shared/tryOnBeforeUnmount/index.md
@@ -0,0 +1,37 @@
---
category: Component
---

# tryOnUnmounted

Safe `onBeforeUnmount`. Call `onBeforeUnmount()` if it's inside a component lifecycle, if not, do nothing

## Usage

```js
import { tryOnBeforeUnmount } from '@vueuse/core'

tryOnBeforeUnmount(() => {

})
```


<!--FOOTER_STARTS-->
## Type Declarations

```typescript
/**
* Call onUnmounted() if it's inside a component lifecycle, if not, do nothing
*
* @param fn
*/
export declare function tryOnBeforeUnmount(fn: Fn): void
```

## Source

[Source](https://github.com/vueuse/vueuse/blob/main/packages/shared/tryOnBeforeUnmount/index.ts) • [Docs](https://github.com/vueuse/vueuse/blob/main/packages/shared/tryOnBeforeUnmount/index.md)


<!--FOOTER_ENDS-->
12 changes: 12 additions & 0 deletions packages/shared/tryOnBeforeUnmount/index.ts
@@ -0,0 +1,12 @@
import { getCurrentInstance, onBeforeUnmount } from 'vue-demi'
import { Fn } from '../utils'

/**
* Call onBeforeUnmount() if it's inside a component lifecycle, if not, do nothing
*
* @param fn
*/
export function tryOnBeforeUnmount(fn: Fn) {
if (getCurrentInstance())
onBeforeUnmount(fn)
}

0 comments on commit f573b95

Please sign in to comment.