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-use-request

v0.3.2

Published

>vue3 hook或者组件的方式使用请求库 ## 安装

Downloads

19

Readme

vue-use-request

vue3 hook或者组件的方式使用请求库

安装

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

使用

hook 方式

<template>
    <div v-if="error">{{error}}</div>
    <div v-else-if="loading">loading...</div>
    <div v-else>{{data}}</div>
</template>
<script>
import { defineComponent }  from 'vue';
import { useRequest } from 'vue-use-request';
export default defineComponent({
    setup() {
        const { data, loading, error } = useRequest(async() => {
            const res = fetch('https://api.github.com/search/repositories?q=vue-use-request').then(res => res.json());
        },
        // config, 配置参数,具体参数配置看下面
        {
            retry: 3,
        });

        return {
            data,
            loading,
            error
        }
    }
})
</script>

参数

    type TimeUnit = 'd' | 'h' | 'm' | 's';

    type StringTime = `${number}${TimeUnit}`


    export type TimeFormat = number | StringTime;

    /**
     * 初始值
     *
     * @type {T}
     * @memberof IConfig
     */
    initalValue?: T,
    /**
     * 当函数内的响应式数据变化时,会重新触发请求
     *
     * @type {boolean}
     * @memberof IConfig
     */
    watch?: MultiWatchSources;
    /**
     * 如果请求失败重试次数
     *
     * @type {number}
     * @memberof IConfig
     */
    retry?: number;
    /**
     * 轮询请求
     *
     * @type {boolean}
     * @memberof IConfig
     */
    polling?: TimeFormat;
    /**
     * 请求去抖动 只取最后一次请求
     *
     * @type {TimeFormat} 比如 100s  2d 3h 3m
     * @memberof IConfig
     */
    debounce?: TimeFormat;
    /**
     * 节流, 在一定时间范围内,取最后一次请求
     *
     * @type {TimeFormat}
     * @memberof IConfig
     */
    throttle?: TimeFormat;
    /**
     * 取缓存数据, 如果为true 则表示永久
     *
     * @type {TimeFormat}
     * @memberof IConfig
     */
    cache?: TimeFormat | boolean;

    /**
     * 惰性运行请求
     *
     * @type {boolean}
     * @memberof IConfig
     */
    lazy?: boolean;

集成axios

使用前需要对 vue 执行 use 方法注入 axios实例和注册组件

import { createApp } from 'vue';
import axios from 'axios';
import { Request } from 'vue-use-request';
const app = createApp();
app.use(Request(axios));

组件方式使用

<template>
    <vue-query url="https://api.github.com/search/repositories" :params="{q: 'vue-use-request'}">
        <template #default="{data, loading, error}">
            <div v-if="error">{{error}}</div>
            <div v-else-if="loading">loading...</div>
            <div v-else>{{data}}</div>
        </template>
    </vue-query>
</template>

hook 方式

<template>
    <div v-if="error">{{error}}</div>
    <div v-else-if="loading">loading...</div>
    <div v-else>{{data}}</div>
</template>
<script>
import { defineComponent }  from 'vue';
import { useQuery } from 'vue-use-request';
export default defineComponent({
    setup() {
        const { data, loading, error } = useQuery('https://api.github.com/search/repositories', { q: 'vue-use-request' });
        return {
            data,
            loading,
            error
        }
    }
})
</script>

TODO

  • [ ] 完善文档
  • [ ] 支持 Mutation 操作