@anweb/nuxt-ancore 1.10.5 → 1.10.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/module.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { computed, ref, watch } from "vue";
|
|
1
|
+
import { computed, ref, watch, onMounted } from "vue";
|
|
2
2
|
import { useAsyncData } from "#app";
|
|
3
3
|
import { toQuery, userApi } from "../utils/index.js";
|
|
4
4
|
export const useAnData = (initConfig) => {
|
|
@@ -6,19 +6,37 @@ export const useAnData = (initConfig) => {
|
|
|
6
6
|
const data = ref(void 0);
|
|
7
7
|
const error = ref(void 0);
|
|
8
8
|
const status = ref("idle");
|
|
9
|
+
const isMounted = ref(false);
|
|
9
10
|
const init = async () => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
11
|
+
if (isMounted.value) {
|
|
12
|
+
const execute = () => {
|
|
13
|
+
status.value = "pending";
|
|
14
|
+
userApi(
|
|
15
|
+
key.value,
|
|
16
|
+
{ method: "GET", ...config.value.apiConfig || {} }
|
|
17
|
+
).then((response) => {
|
|
18
|
+
status.value = "success";
|
|
19
|
+
data.value = response;
|
|
20
|
+
}).catch((e) => {
|
|
21
|
+
status.value = "error";
|
|
22
|
+
error.value = e;
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
watch(() => key.value, execute, { immediate: true, deep: true });
|
|
26
|
+
} else {
|
|
27
|
+
const Data = useAsyncData(
|
|
28
|
+
key,
|
|
29
|
+
() => userApi(
|
|
30
|
+
key.value,
|
|
31
|
+
{ method: "GET", ...config.value.apiConfig || {} }
|
|
32
|
+
),
|
|
33
|
+
{ immediate: false }
|
|
34
|
+
);
|
|
35
|
+
await Data.execute();
|
|
36
|
+
watch(Data.data, () => data.value = Data.data.value, { immediate: true });
|
|
37
|
+
watch(Data.error, () => error.value = Data.error.value, { immediate: true });
|
|
38
|
+
watch(Data.status, () => status.value = Data.status.value, { immediate: true });
|
|
39
|
+
}
|
|
22
40
|
};
|
|
23
41
|
const set = (value) => {
|
|
24
42
|
data.value = value;
|
|
@@ -35,6 +53,9 @@ export const useAnData = (initConfig) => {
|
|
|
35
53
|
return url + query;
|
|
36
54
|
});
|
|
37
55
|
const loading = computed(() => status.value === "pending");
|
|
56
|
+
onMounted(() => {
|
|
57
|
+
isMounted.value = true;
|
|
58
|
+
});
|
|
38
59
|
return {
|
|
39
60
|
init,
|
|
40
61
|
set,
|
|
@@ -8,7 +8,7 @@ interface TFormParams<TForm> {
|
|
|
8
8
|
structure: Record<keyof TForm, TStructureItem<TForm[keyof TForm]>>;
|
|
9
9
|
data?: Partial<TForm> | null;
|
|
10
10
|
}
|
|
11
|
-
export declare const useAnForm: <TForm extends
|
|
11
|
+
export declare const useAnForm: <TForm extends object>(params: TFormParams<TForm>) => {
|
|
12
12
|
state: [TForm] extends [import("vue").Ref<any, any>] ? import("@vue/shared").IfAny<TForm, import("vue").Ref<TForm, TForm>, TForm> : import("vue").Ref<import("vue").UnwrapRef<TForm>, TForm | import("vue").UnwrapRef<TForm>>;
|
|
13
13
|
merge: (data: TForm) => void;
|
|
14
14
|
validator: {
|
|
@@ -4,7 +4,14 @@ import {
|
|
|
4
4
|
useAsyncValidator
|
|
5
5
|
} from "@vueuse/integrations/useAsyncValidator";
|
|
6
6
|
export const useAnForm = (params) => {
|
|
7
|
-
const state = ref(
|
|
7
|
+
const state = ref(
|
|
8
|
+
Object.fromEntries(
|
|
9
|
+
Object.entries(params.structure).map(([k, v]) => {
|
|
10
|
+
const key = k;
|
|
11
|
+
return [key, params.data?.[key] ?? v.default];
|
|
12
|
+
})
|
|
13
|
+
)
|
|
14
|
+
);
|
|
8
15
|
const keys = Object.keys(state.value);
|
|
9
16
|
let validatorExecute;
|
|
10
17
|
const History = useRefHistory(state, { deep: true });
|