-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
onMountedかonActivatedのどちらかが呼ばれた時に実行されるコンポーザブルを定義 (#1890)
* onMountedかonActivatedのどちらかが呼ばれた時に実行されるコンポーザブルを定義 * useOnRenderingにしてみた * 名称を変更 * 条件式が違ってただけだった * ニ通りの書き方で実装してみた * ややこしいので1つにした * 問題はここではなかったかも * 不要なの
- Loading branch information
Showing
4 changed files
with
64 additions
and
59 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
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { onMounted, onActivated, onDeactivated, onUnmounted } from "vue"; | ||
|
||
/** | ||
* onMountedかonActivatedのどちらかが呼ばれた時に実行される関数を登録する。 | ||
* | ||
* NOTE: | ||
* 大抵の場合はonMountedのあとにonActivatedが必ず呼ばれるが、 | ||
* KeepAliveでonActivatedが呼ばれるべきタイミングを過ぎた後、 | ||
* v-ifなどで切り替わるとonMountedのみが呼ばれる場合がある。 | ||
* このケースに対応している。 | ||
*/ | ||
export const onMountedOrActivated = (hook: () => void) => { | ||
let shouldCall = true; | ||
|
||
const start = () => { | ||
if (shouldCall) { | ||
hook(); | ||
shouldCall = false; | ||
} | ||
}; | ||
onMounted(start); | ||
onActivated(start); | ||
|
||
const stop = () => { | ||
shouldCall = true; | ||
}; | ||
onDeactivated(stop); | ||
onUnmounted(stop); | ||
}; | ||
|
||
/** | ||
* onUnmountedかonDeactivatedのどちらかが呼ばれた時に実行される関数を登録する。 | ||
*/ | ||
export const onUnmountedOrDeactivated = (hook: () => void) => { | ||
let shouldCall = false; | ||
|
||
const start = () => { | ||
shouldCall = true; | ||
}; | ||
onMounted(start); | ||
onActivated(start); | ||
|
||
const stop = () => { | ||
if (shouldCall) { | ||
hook(); | ||
shouldCall = false; | ||
} | ||
}; | ||
onUnmounted(stop); | ||
onDeactivated(stop); | ||
}; |