Ben Lin
2025-01-01 3f3817a39238b262155cd5ec76fa351bb344602d
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { computed, defineComponent, reactive, ref, unref } from 'vue';
import { Button, Card, Typography, Select } from 'ant-design-vue';
import { imitateApi } from './mock-api';
import { useRequest } from '@vben/hooks';
import { PageWrapper } from '@/components/Page';
 
const options = [
  { label: 'Jack', value: 'Jack' },
  { label: 'Lucy', value: 'Lucy' },
  { label: 'Lutz', value: 'Lutz' },
];
 
const Demo1 = defineComponent({
  setup() {
    const select = ref('Lutz');
    const { data, loading } = useRequest(() => imitateApi(select.value), { refreshDeps: [select] });
 
    return () => (
      <Card title="Ref 依赖刷新">
        <Typography>
          <Typography.Paragraph>
            useRequest 提供了一个
            <Typography.Text type="danger"> options.refreshDeps </Typography.Text>
            参数,当它的值变化后,会重新触发请求。
          </Typography.Paragraph>
          <Typography.Paragraph>
            <Typography.Text>
              <pre>
                {`const select = ref('Lutz');
const { data, loading } = useRequest(() => imitateApi(select.value), {
  refreshDeps: [select]
});`}
              </pre>
            </Typography.Text>
          </Typography.Paragraph>
        </Typography>
 
        <Select
          v-model={[select.value, 'value']}
          options={options}
          style="width: 220px"
          disabled={loading.value}
        />
        <p>Username: {loading.value ? 'Loading' : unref(data)}</p>
      </Card>
    );
  },
});
 
const Demo2 = defineComponent({
  setup() {
    const numOrign = ref(1);
    const changeNum = () => {
      ++numOrign.value;
    };
    const numComp = computed(() => numOrign.value * Math.PI);
 
    const { data, loading } = useRequest(imitateApi, { refreshDeps: [numComp] });
 
    return () => (
      <Card title="Computed 依赖刷新" class="mt-2">
        <Typography>
          <Typography.Paragraph>
            <Typography.Text type="danger"> options.refreshDeps </Typography.Text>
            可以是计算类型
          </Typography.Paragraph>
          <Typography.Paragraph>
            <Typography.Text>
              <pre>
                {`const numOrign = ref(1);
const changeNum = () => {
  ++numOrign.value;
};
const numComp = computed(() => numOrign.value * Math.PI);
 
const { data, loading } = useRequest(imitateApi, {
  refreshDeps: [numComp]
});`}
              </pre>
            </Typography.Text>
          </Typography.Paragraph>
        </Typography>
 
        <p>Username: {loading.value ? 'Loading' : unref(data)}</p>
        <Button type="primary" onClick={changeNum} disabled={loading.value}>
          changeNum
        </Button>
      </Card>
    );
  },
});
 
const Demo3 = defineComponent({
  setup() {
    const status = reactive({ id: 'lutz' });
    const changeStatus = () => {
      status.id = status.id === 'LUTZ' ? 'lutz' : 'LUTZ';
    };
    const { data, loading } = useRequest(imitateApi, { refreshDeps: [() => status.id] });
 
    return () => (
      <Card title="Function 依赖刷新" class="mt-2">
        <Typography>
          <Typography.Paragraph>
            <Typography.Text type="danger"> options.refreshDeps </Typography.Text>
            可以是一个方法,但必须是一个关于响应型数据 Reactive、Ref、Computed 的 effect 函数
          </Typography.Paragraph>
 
          <Typography.Paragraph>
            <Typography.Text>
              <pre>
                {`const status = reactive({ id: 'lutz' });
const changeStatus = () => {
  status.id = status.id === 'LUTZ' ? 'lutz' : 'LUTZ';
};
 
const { data, loading } = useRequest(imitateApi, {
  refreshDeps: [() => status.id]
});`}
              </pre>
            </Typography.Text>
          </Typography.Paragraph>
        </Typography>
 
        <p>Username: {loading.value ? 'Loading' : unref(data)}</p>
        <Button type="primary" onClick={changeStatus} disabled={loading.value}>
          changeStatus
        </Button>
      </Card>
    );
  },
});
 
export default defineComponent({
  setup() {
    return () => (
      <PageWrapper
        title="依赖刷新"
        v-slots={{
          headerContent: () => (
            <Typography>
              <Typography.Paragraph>
                useRequest 提供了
                <Typography.Text type="danger"> options.refreshDeps </Typography.Text>
                参数,当它的值变化后,会重新触发请求。
              </Typography.Paragraph>
              <Typography.Paragraph>
                <Typography.Text>
                  <pre>
                    {`import type { WatchSource } from 'vue';
 
// useRequestOption
refreshDeps?: WatchSource<any>[];
refreshDepsAction?: () => void; // 非手动执行, 默认执行 fetchInstance.refresh`}
                  </pre>
                </Typography.Text>
              </Typography.Paragraph>
            </Typography>
          ),
        }}
      >
        <Demo1 />
        <Demo2 />
        <Demo3 />
      </PageWrapper>
    );
  },
});