Ben Lin
2024-06-18 ebbd788fbb2c0b45d4473798efc57eec8ba74a25
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
import { defineComponent, ref } from 'vue';
import { Card, Typography, Input, Spin, Space } from 'ant-design-vue';
import { imitateApi } from './mock-api';
import { useRequest } from '@vben/hooks';
import { PageWrapper } from '@/components/Page';
 
const Demo1 = defineComponent({
  setup() {
    const search = ref('');
 
    const { data, loading } = useRequest(imitateApi, {
      throttleWait: 1000,
      refreshDeps: [search],
    });
 
    return () => (
      <Card title="节流">
        <Typography>
          <Typography.Paragraph>
            通过设置
            <Typography.Text type="danger"> options.throttleWait </Typography.Text>
            ,进入节流模式,此时如果频繁触发
            <Typography.Text code> run </Typography.Text>或者
            <Typography.Text code> runAsync </Typography.Text>, 则会以节流策略进行请求。
          </Typography.Paragraph>
 
          <Typography.Paragraph>
            <Typography.Text code>
              {`const { data, run } = useRequest(imitateApi, { throttleWait: 300, manual: true });`}
            </Typography.Text>
          </Typography.Paragraph>
 
          <Typography.Paragraph>
            如上示例代码,频繁触发
            <Typography.Text code> run </Typography.Text>, 300ms 执行一次。
          </Typography.Paragraph>
 
          <Typography.Paragraph>你可以在下面 input 框中快速输入文本,体验效果</Typography.Paragraph>
        </Typography>
 
        {/* 节流 */}
        <Spin spinning={loading.value}>
          <Space direction="vertical">
            <Input v-model={[search.value, 'value']} placeholder="Please enter username" />
            <div>Username: {data.value}</div>
          </Space>
        </Spin>
      </Card>
    );
  },
});
 
export default defineComponent({
  setup() {
    return () => (
      <PageWrapper>
        <Demo1 />
      </PageWrapper>
    );
  },
});