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

@vruse/axios

v1.1.25

Published

Collection of essential Vue Composition Utilities

Downloads

3

Readme

useAxios

Axios 的封装,既可以不带 await 使用,也可以带 await 使用

如果使用 const { data } = useAxios('url') 则 data 为一个 vue 响应式变量,初始值为 undefined,等待请求完成后自动变成请求结果,适用于非异步函数情景。

如果使用 const { data } = await useAxios('url') 则 data 直接为请求结果。

使用方式

不带 await

<script setup lang=ts>
import { useAxios } from '@vruse/axios'

const { data, loading } = useAxios('https://wwww.demo.com/api')
</script>

<template>
  <div v-if="loading">
    loading...
  </div>
  <pre v-else>
    {{ data }}
  </pre>
</template>

异步模式使用

<script setup lang=ts>
import { useAxios } from '@vruse/axios'
const res = ref()
const loading = ref(true)

async function run() {
  const { data } = await useAxios('https://wwww.demo.com/api')
  res.value = data.value
  loading.value = false
}
run()
</script>

<template>
  <div v-if="loading">
    loading...
  </div>
  <pre v-else>
    {{ data }}
  </pre>
</template>

使用外部自定义数据控制源

如果您觉得 useAxios 返回的 data 和 loading 还需要再次赋值给自己的源数据,比较繁琐,那么你可以提前准备好数据源,在请求时传入,我们可以将上一个示例修改为:

<script setup lang=ts>
import { useAxios } from '@vruse/axios'
const res = ref()
const loading = ref(true)

async function run() {
  await useAxios('https://wwww.demo.com/api', {
    controller: {
      data: res,
      loading
    }
  })
}
run()
</script>

<template>
  <div v-if="loading">
    loading...
  </div>
  <pre v-else>
    {{ data }}
  </pre>
</template>

取消请求

<script setup lang=ts>
import { useAxios } from '@vruse/axios'

const { data, loading, adbort } = useAxios('https://wwww.demo.com/api')
// 1000 后若还没有请求成功就取消
setTimeOut(adbort, 1000)
</script>

<template>
  <div v-if="loading">
    loading...
  </div>
  <pre v-else>
    {{ data }}
  </pre>
</template>

自定义 axios 实例

import { useAxiosCreate } from '@vruse/axios'

// 可接受 axios 的配置对象
// 参考 https://axios-http.com/docs/req_config
useAxiosCreate({
  baseUrl: 'https://demo.com'
})
// 后续的 useAxios 都会使用这个实例

维护多个实例

useAxios 默认使用最后一次创建的实例,如果您有维护多个 Axios 实例的需求,那么你可以多次调用 useAxiosCreate(),然后在调用 useAxios 时手动声明使用哪一个实例:

import { useAxiosCreate } from '@vruse/axios'

// 可接受 axios 的配置对象
// 参考 https://axios-http.com/docs/req_config
const instance1 = useAxiosCreate({
  baseUrl: 'https://demo.com'
})

const instance2 = useAxiosCreate({
  baseUrl: 'https://demo2.com'
})

// 后续的 useAxios 默认会使用最后一次创建的实例
// 手动声明使用 instance1
useAxios('/api', {
  controller: {
    instance: instance1
  }
})

获取 axios 实例

import { useAxiosInstance } from '@vruse/axios'
const instance = useAxiosInstance()
// Alter defaults after instance has been created
instance.defaults.headers.common.Authorization = AUTH_TOKEN

修改 axios 配置

获取实例后修改即可

import { useAxiosInstance } from '@vruse/axios'
const instance = useAxiosInstance()
// Alter defaults after instance has been created
instance.defaults.headers.common.Authorization = AUTH_TOKEN

请求拦截,相应拦截

获取实例后修改即可 参考:Axios 文档

import { useAxiosInstance } from '@vruse/axios'
const instance = useAxiosInstance()
// 请求拦截
instance.interceptors.request.use(() => { /* ...*/ })
// 相应拦截
instance.interceptors.response.use(() => { /* ...*/ })