-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathtemplate-async.vue
107 lines (102 loc) · 2.14 KB
/
template-async.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<template>
<Form :form="form">
<Field
name="linkage"
title="联动选择框"
:decorator="[FormItem]"
:component="[
Select,
{
style: {
width: '240px',
},
},
]"
:dataSource="[
{ label: '发请求1', value: 1 },
{ label: '发请求2', value: 2 },
]"
/>
<Field
name="select"
title="异步选择框"
:decorator="[FormItem]"
:component="[
Select,
{
style: {
width: '240px',
},
},
]"
/>
<Submit @submit="onSubmit">提交</Submit>
</Form>
</template>
<script>
import { createForm, onFieldReact } from '@formily/core'
import { Field } from '@formily/vue'
import { action } from '@formily/reactive'
import { Form, FormItem, Select, Submit, Reset } from '@formily/element'
const useAsyncDataSource = (pattern, service) => {
onFieldReact(pattern, (field) => {
field.loading = true
service(field).then(
action.bound((data) => {
field.dataSource = data
field.loading = false
})
)
})
}
const form = createForm({
effects: () => {
useAsyncDataSource('select', async (field) => {
const linkage = field.query('linkage').get('value')
if (!linkage) return []
return new Promise((resolve) => {
setTimeout(() => {
if (linkage === 1) {
resolve([
{
label: 'AAA',
value: 'aaa',
},
{
label: 'BBB',
value: 'ccc',
},
])
} else if (linkage === 2) {
resolve([
{
label: 'CCC',
value: 'ccc',
},
{
label: 'DDD',
value: 'ddd',
},
])
}
}, 1500)
})
})
},
})
export default {
components: { Form, Field, Submit, Reset },
data() {
return {
form,
FormItem,
Select,
}
},
methods: {
onSubmit(value) {
console.log(value)
},
},
}
</script>