提交 | 用户 | age
|
2f6253
|
1 |
import { |
陈 |
2 |
watch, |
|
3 |
computed, |
|
4 |
WatchSource, |
|
5 |
getCurrentInstance, |
|
6 |
onMounted, |
|
7 |
onUnmounted, |
|
8 |
nextTick, |
|
9 |
reactive, |
81baf1
|
10 |
ComponentInternalInstance, |
2f6253
|
11 |
} from 'vue'; |
4ff1c4
|
12 |
import { error } from '../log'; |
2f6253
|
13 |
|
陈 |
14 |
export function explicitComputed<T, S>(source: WatchSource<S>, fn: () => T) { |
|
15 |
const v = reactive<any>({ value: fn() }); |
|
16 |
watch(source, () => (v.value = fn())); |
|
17 |
return computed<T>(() => v.value); |
|
18 |
} |
|
19 |
|
|
20 |
export function tryOnMounted(fn: () => void, sync = true) { |
|
21 |
if (getCurrentInstance()) { |
|
22 |
onMounted(fn); |
|
23 |
} else if (sync) { |
|
24 |
fn(); |
|
25 |
} else { |
|
26 |
nextTick(fn); |
|
27 |
} |
|
28 |
} |
|
29 |
|
|
30 |
export function tryOnUnmounted(fn: () => Promise<void> | void) { |
db0bfc
|
31 |
getCurrentInstance() && onUnmounted(fn); |
2f6253
|
32 |
} |
陈 |
33 |
|
81baf1
|
34 |
export function tryTsxEmit<T extends any = ComponentInternalInstance>( |
V |
35 |
fn: (_instance: T) => Promise<void> | void |
|
36 |
) { |
|
37 |
const instance = getCurrentInstance() as any; |
db0bfc
|
38 |
instance && fn.call(null, instance); |
2f6253
|
39 |
} |
陈 |
40 |
|
|
41 |
export function isInSetup() { |
|
42 |
if (!getCurrentInstance()) { |
4ff1c4
|
43 |
error('Please put useForm function in the setup function!'); |
2f6253
|
44 |
} |
陈 |
45 |
} |