版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 6.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. English | [简体中文](README-zh_CN.md)
  2. <p align="center">
  3. <a href="https://www.attojs.org">
  4. <img
  5. width="150"
  6. src="https://raw.githubusercontent.com/AttoJS/art/master/vue-request-logo.png"
  7. alt="VueRequest logo"
  8. />
  9. </a>
  10. </p>
  11. <h1 align="center">VueRequest</h1>
  12. <div align="center">
  13. <p align="center">⚡️ Vue 3 composition API for data fetching, supports SWR, polling, error retry, cache request, pagination, etc.</p>
  14. <a href="https://codecov.io/github/attojs/vue-request?branch=master">
  15. <img
  16. src="https://img.shields.io/codecov/c/github/attojs/vue-request?token=NW2XVQWGPP"
  17. alt="Coverage Status"
  18. />
  19. </a>
  20. <a href="https://www.npmjs.com/package/vue-request">
  21. <img src="https://img.shields.io/bundlephobia/minzip/vue-request/latest" alt="Size" />
  22. </a>
  23. <a href="https://www.npmjs.com/package/vue-request">
  24. <img src="https://img.shields.io/npm/v/vue-request" alt="Version" />
  25. </a>
  26. <a href="https://www.npmjs.com/package/vue-request">
  27. <img src="https://img.shields.io/github/languages/top/attojs/vue-request" alt="Languages" />
  28. </a>
  29. <a href="https://www.npmjs.com/package/vue-request">
  30. <img src="https://img.shields.io/npm/l/vue-request" alt="License" />
  31. </a>
  32. <a href="https://github.com/AttoJS/vue-request/stargazers">
  33. <img src="https://img.shields.io/github/stars/attojs/vue-request" alt="Star" />
  34. </a>
  35. <a href="https://www.npmjs.com/package/vue-request">
  36. <img src="https://img.shields.io/npm/dm/vue-request" alt="Download" />
  37. </a>
  38. </div>
  39. ## Why VueRequest
  40. In the past projects, they were often confused by repeated implementations such as the management of the loading state, the requested throttling and debounce, the caching of request data, and pagination. Whenever we start a new project, we have to manually deal with the above problems, which will be a repetitive work, but also to ensure that the team is consistent.
  41. VueRequest aims to provide developers with a convenient and fast way to manage the state of the request API. In the development, save repetitive work, and it can be used only with a simple configuration, focusing on the core of the development project.
  42. ## Features
  43. - 🚀 &nbsp;All data is reactive
  44. - 🔄 &nbsp;Interval polling
  45. - 🤖 &nbsp;Automatic error retry
  46. - 🗄 &nbsp;Built-in cache
  47. - 💧 &nbsp;Throttle and Debounce
  48. - ⚙️ &nbsp;Powerful pagination extension and load more extensions
  49. - 📠 &nbsp;Written in TypeScript
  50. - ⚡️ &nbsp;Compatible with Vite
  51. - 🍃 &nbsp;Lightweight
  52. - 📦 &nbsp;Out of the box
  53. ## Documentation
  54. - [English](https://www.attojs.org/)
  55. - [中文文档](https://www.attojs.com/)
  56. ## Install
  57. You can install VueRequest with [NPM](https://www.npmjs.com/), [YARN](https://yarnpkg.com/), or a `<script>` via [unpkg.com](https://unpkg.com/)
  58. ### NPM
  59. ```sh
  60. npm install vue-request
  61. # or
  62. yarn add vue-request
  63. ```
  64. ### CDN
  65. > For production, we recommend linking to a specific version number and build to avoid unexpected breakage from newer versions.
  66. ```html
  67. <script src="https://unpkg.com/vue-request/dist/vue-request.min.js"></script>
  68. ```
  69. Once you've added this you will have access to the `window.VueRequest` object and its exports.
  70. ## Usage
  71. ```vue
  72. <template>
  73. <div>
  74. <div v-if="loading">loading...</div>
  75. <div v-if="error">failed to fetch</div>
  76. <div v-if="data">Hey! {{ data }}</div>
  77. </div>
  78. </template>
  79. <script lang="ts">
  80. import { defineComponent } from 'vue';
  81. export default defineComponent({
  82. setup() {
  83. const { data, loading, error } = useRequest(service);
  84. return {
  85. data,
  86. loading,
  87. error,
  88. };
  89. },
  90. });
  91. </script>
  92. ```
  93. In this example, `useRequest` accepts a `service` function. `service` is a asynchronous function. In other words, you can use **axios** to fetch data and return a **Promise**. More specific instructions can be viewed in [document](https://www.attojs.org/guide/documentation/dataFetching.html).
  94. `useRequest` also return 3 values: `data`, `loading` and `error`. When the request is not yet finished, data will be `undefined` and `loading` will be `true`. And when we get a response, it sets data and error based on the result of service and rerenders the component. This is because `data` and `error` are [Reactivity(Refs)](https://v3.vuejs.org/guide/reactivity-fundamentals.html), and their values will be set by the service response.
  95. ## Some of the coolest features:
  96. VueRequest has many features, such as error retry, cache, pagination, throttle, debounce..., here are two cool features
  97. ### 1.Refresh On Focus
  98. Sometimes, you need to ensure data consistency between multiple browser windows; or when the user's computer is reactivated in the dormant state, the page data needs to be synchronized to the latest state. `refreshOnWindowFocus` may save you a lot of code. [Click here to go to the document](https://www.attojs.org/guide/documentation/refreshOnWindowFocus.html)
  99. ```ts
  100. const { data, error, run } = useRequest(getUserInfo, {
  101. refreshOnWindowFocus: true,
  102. refocusTimespan: 1000, // refresh interval 1s
  103. });
  104. ```
  105. ![vue-request](https://z3.ax1x.com/2021/09/10/hXAs8s.gif)
  106. ### 2.Polling Data
  107. Sometimes, you want to ensure that data is synchronized and updated between multiple devices. At this time, we can use the `pollingInterval` provided by us to periodically re-request the request API, so that the data consistency between multiple devices can be guaranteed. When the user modifies the data, the two windows will be updated simultaneously in real time. [Click here to go to the document](https://www.attojs.org/guide/documentation/polling.htm)
  108. ```ts
  109. const { data, error, run } = useRequest(getUserInfo, {
  110. pollingInterval: 1000, // polling interval 1s
  111. });
  112. ```
  113. ![vue-request](https://z3.ax1x.com/2021/09/10/hXAy2n.gif)
  114. ## TODO List
  115. If you have any cool features, please submit an issue for discussion
  116. - [ ] Support Vue 2
  117. - [x] Documentation
  118. - [x] Pagination
  119. - [x] Load More
  120. ## Thanks
  121. Thank them for inspiring us.
  122. - [vercel/swr](https://github.com/vercel/swr)
  123. - [alibaba/hooks](https://ahooks.js.org/hooks/async#userequest)
  124. ## License
  125. [MIT License](https://github.com/AttoJS/vue-request/blob/master/LICENSE) © 2020-present [AttoJS](https://github.com/AttoJS)