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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ajax-plus

v0.1.1

Published

<a href="https://www.npmjs.com/package/ajax-plus"><img src="https://img.shields.io/badge/npm-v0.1.0-blue.svg" alt="Version"> </a>

Downloads

12

Readme

ajax-plus

基于axios 的 Vue 插件

如何使用

npm 模块引入

首先通过 npm 安装

npm install --save ajax-plus
or
yarn add ajax-plus -S

然后在入口文件引入并配置:

对标axios的配置,详见axios🚀

import Vue from 'Vue'
// 引入
import ajaxPlus from 'ajax-plus'
// 配置
Vue.use(ajaxPlus, {
    //这里写一些ajax的option,详见axios文档,比如
    baseURL: "https://jsonplaceholder.typicode.com",
    timeout: 150000
})

示例

$ajaxPlus方法

在 Vue 组件上添加了 $ajaxPlus 方法, 使用如下:

// method可以为 get、delete、options、post、put、patch、head

// url为去除baseUrl的

// data为object

this.$ajaxPlus(method, url, data, res =>{
    //success call back code
})

//也可以省略data参数,直接写callback(鉴于有些请求不需要传参数)
this.$ajaxPlus(method, url, res =>{
    //success call back code
})

//$ajaxPlus已经在源码中处理catch容错了,假若想在代码里处理报错,再加一个参数,如下

this.$ajaxPlus(method, url, data, res =>{
    //success call back code
},{
    //catch是ajax请求失败后 要执行的代码
    //finallyCb是ajax请求结束后 要执行的代码,无论成功或者失败
    catchCb:()=>{//code}    
    finallyCb:()=>{//code}
})

以上catchCbfinallyCb几乎很少会用

ajax-plus中给vue全局mixin了一个loading变量,会在ajax请求结束后自动置为false,这个变量,你可以做一些ui层,比如按钮的防止高频功能

如果你还要做其它相关操作 可以写在finallyCb中.

比如

<el-button :loading="loading1" @click="handleSubmit">按钮1</el-button>
<el-button :loading="loading2">按钮2</el-button>
handleSubmit(){
    this.$ajaxPlus('post','/submit',{foo:1, bar:2}, res=>{
        alert('提交成功了')
    },{
        catchCb:()=>{
            alert('提交失败了')
        },    
        finallyCb:()=>{
            //按钮置为可点击状态
            this.loading1 = false;
            this.loading2 = false;
        }
    })
}

$ajax

也可以通过 this.$axios 来使用 axios 所有的 api 方法,如下:

this.$ajax.get(url, data).then(res =>{
  //拿到res了
})

this.$ajax.post(url, data).then( res =>{
  //拿到res了
})

try {
  const data = await this.$ajax.post(url, data)
} catch (error) {
  
}

由于前后端约定不一致,关于callback的更深层的处理并没有完善。

axiosroutervuex结合起来才能更强大,比如拦截器中根据status判断是否登陆,用户的鉴权可以和store结合,response的相关报错和相关ui的Diag、Message结合会更棒.

更多使用方式以及改写, 参考代码, 欢迎提意见。

License

MIT