npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-request

v2.0.4

Published

This is a library that can easily help you manage request states, supporting common features such as SWR, polling, error retry, caching, and pagination, etc.

Downloads

12,856

Readme

English | 简体中文

为什么选择 VueRequest

在以往的业务项目中,我们经常会被 loading 状态的管理、请求的节流防抖、接口数据的缓存、分页等重复的功能实现所困扰。每次开启一个新项目都需要重新实现一遍,这是一项重复的工作,还需要确保团队的一致性。

VueRequest 的目的是为开发人员提供一种方便、快速的方式来管理 API 状态。通过简单的配置,可以省去那些繁琐的任务,专注于业务核心的开发。

特性

  • 🌈  兼容 Vue 2 & 3
  • 🚀  所有数据都具有响应式
  • 🔄  轮询请求
  • 🤖  自动处理错误重试
  • 🗄  内置请求缓存
  • 💧  节流请求与防抖请求
  • 🎯  聚焦页面时自动重新请求
  • ⚙️  强大的分页扩展以及加载更多扩展
  • 📠  完全使用 Typescript 编写,具有强大的类型提示
  • ⚡️  兼容 Vite
  • 🍃  轻量化
  • 📦  开箱即用

文档

安装

你可以通过 NPMYARN 或者通过 <script> 的方式引入 unpkg.com 上的包。

NPM

npm install vue-request
# or
yarn add vue-request
# or
pnpm install vue-request

CDN

对于生产环境,我们推荐链接到一个明确的版本号和构建文件,以避免新版本造成的不可预期的破坏。

<script src="https://unpkg.com/vue-request/dist/vue-request.min.js"></script>

一旦你在页面中添加了它,你就可以在 window.VueRequest 中访问我们导出的方法。

示例

<template>
  <div>
    <div v-if="loading">loading...</div>
    <div v-if="error">failed to fetch</div>
    <div v-if="data">Hey! {{ data }}</div>
  </div>
</template>

<script lang="ts" setup>
const { data, loading, error } = useRequest(service);
</script>

在这个例子中,useRequest 接收了一个 service 函数。service是一个异步的请求函数,换句话说,你可以使用 axios 来获取数据,然后返回一个 Promise。更具体的说明可以在文档中查看。

useRequest 函数还会返回三个值:dataloadingerror。当请求还未完成时,data 的值为 undefined,同时 loading 的值会被设置为 true。当请求完成后,dataerror 的值将根据请求结果进行设置,并且页面也会相应地进行渲染。这是因为 dataloadingerror 是 Vue 中的响应式引用(Refs),它们的值会根据请求状态和结果进行修改。

一些很酷的特性

VueRequest 提供了很多特性,如:错误重试、缓存、分页、节流、防抖等等。这里列举两个比较酷的特性:

1.聚焦页面时自动重新请求

有时,你需要确保多个浏览器窗口之间的数据保持一致性;或者在用户电脑从休眠状态中恢复并重新激活时,需要将页面的数据同步到最新状态。使用 refreshOnWindowFocus 可以帮助你节省很多逻辑代码。点击这里直达文档

const { data, error, run } = useRequest(getUserInfo, {
  refreshOnWindowFocus: true,
  refocusTimespan: 1000, // 请求间隔时间
});

vue-request

2.轮询数据

有时候,你需要确保多个设备之间的数据同步更新。这时候可以使用我们提供的 pollingInterval 定期重新请求接口,以确保多个设备之间的数据一致性。当用户修改数据时,两个窗口将会实时同步更新。点击这里直达文档

const { data, error, run } = useRequest(getUserInfo, {
  pollingInterval: 1000, // 请求间隔时间
});

vue-request

致谢

感谢他们为我们提供了灵感

License

MIT License © 2020-present AttoJS