All Projects → egoist → Vue Fetch Data

egoist / Vue Fetch Data

Licence: mit
A simple and declarative way to fetch data for Vue components.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Vue Fetch Data

Vue Video
vue + vue-router + vuex + (fetch->axios)
Stars: ✭ 251 (+286.15%)
Mutual labels:  axios, fetch
beccaccino
Beccaccino is an easy, sexy, reliable, framework agnostic http client for redux that is ⚡️beccaccino fast!
Stars: ✭ 13 (-80%)
Mutual labels:  fetch, axios
Mande
600 bytes convenient and modern wrapper around fetch
Stars: ✭ 154 (+136.92%)
Mutual labels:  axios, fetch
React Ufo
🛸 react-ufo - A simple React hook to help you with data fetching 🛸
Stars: ✭ 85 (+30.77%)
Mutual labels:  axios, fetch
fetchx
Beautiful way to fetch data in React
Stars: ✭ 71 (+9.23%)
Mutual labels:  fetch, axios
vue-methods-promise
Let Vue methods support return Promise
Stars: ✭ 35 (-46.15%)
Mutual labels:  fetch, axios
miniprogram-network
Redefine the Network API of Wechat MiniProgram (小程序网络库)
Stars: ✭ 93 (+43.08%)
Mutual labels:  fetch, axios
Redux Requests
Declarative AJAX requests and automatic network state management for single-page applications
Stars: ✭ 330 (+407.69%)
Mutual labels:  axios, fetch
fetchingInReact
💎📷 Fetching data from Unsplash.com in React
Stars: ✭ 23 (-64.62%)
Mutual labels:  fetch, axios
Code-VueWapDemo
“Vue教程--Wap端项目搭建从0到1”的源码
Stars: ✭ 19 (-70.77%)
Mutual labels:  fetch, axios
Thwack
A tiny modern data fetching solution
Stars: ✭ 268 (+312.31%)
Mutual labels:  axios, fetch
Frisbee
🐕 Modern fetch-based alternative to axios/superagent/request. Great for React Native.
Stars: ✭ 1,038 (+1496.92%)
Mutual labels:  axios, fetch
Vue axios spa
基于vue2+axios+vux+vue-router+vuex构建的单页微信端项目
Stars: ✭ 54 (-16.92%)
Mutual labels:  axios
Nuxt Netlify Functions Example
Nuxt.js example for running Netlify functions locally in a dev environment and as a generated static site deployed to Netlify
Stars: ✭ 59 (-9.23%)
Mutual labels:  axios
Vue Viewplus
一个简化Vue应用开发的工具库
Stars: ✭ 54 (-16.92%)
Mutual labels:  axios
Cross Fetch
Universal WHATWG Fetch API for Node, Browsers and React Native.
Stars: ✭ 1,063 (+1535.38%)
Mutual labels:  fetch
Django React
This a simple Django and React demo application
Stars: ✭ 63 (-3.08%)
Mutual labels:  axios
Mall Admin Web
mall-admin-web是一个电商后台管理系统的前端项目,基于Vue+Element实现。 主要包括商品管理、订单管理、会员管理、促销管理、运营管理、内容管理、统计报表、财务管理、权限管理、设置等功能。
Stars: ✭ 9,123 (+13935.38%)
Mutual labels:  axios
Redux Query
A library for managing network state in Redux
Stars: ✭ 1,055 (+1523.08%)
Mutual labels:  fetch
React Async Fetcher
React component for asynchronous loading/fetch online data
Stars: ✭ 50 (-23.08%)
Mutual labels:  fetch

vue-fetch-data

NPM version NPM downloads Build Status donate

A simple and declarative way to fetch data for Vue components.

Features

  • Small-size, only 800 bytes gzipped
  • Fetch and compose data in a declarative way

Install

yarn add vue-fetch-data

Usage

An example component which fetches a GitHub user:

import Vue from 'vue'
import FetchData from 'vue-fetch-data'

Vue.use(FetchData)
<template>
  <div>
    <div v-if="user.pending">Loading...</div>
    <div v-if="user.fulfilled">{{ user.value.login }}</div>
    <div v-if="user.rejected">{{ user.reason.message }}</div>
  </div>
</template>

<script>
  export default {
    props: ['username'],
    // make sure you set the initial value of that property to `{}`
    data: () => ({ user: {} }),
    fetch: {
      user: vm => `https://api.github.com/users/${vm.username}`
    }
  }
</script>

Then boom, check out live demo at https://vue-fetch-data.surge.sh

fetch

The fetch in component options is an Object which is similar to computed option.

Examples

The value of each entry can be string Object or a function which returns those. The returned Object could contain any axios option, the returned string will be used as url and fetched in GET method.

export default {
  fetch: {
    user() {
      return `https://api.github.com/users/${this.username}`
    },
    article: 'https://get-article-api.com/api/get_post',
    users: {
      url: 'https://get-users/api/users',
      method: 'POST',
      data: {
        offset: 20
      }
    }
  }
}

poll

Refetch in every poll ms:

export default {
  fetch: {
    posts: vm => ({
      url: '/api/posts',
      poll: 1000, // update every second
      params: {
        limit: vm.limit,
        offset: vm.offset
      }
    })
  }
}

commit

Instead of updating data in current component, you can use commit to commit a Vuex mutation:

export default {
  computed: {
    ...mapState(['user'])
  },
  fetch: {
    user: {
      commit: 'UPDATE_USER',
      url: '/user/egoist'
    }
  }
}

And your vuex store would look like:

{
  state: {
    user: {}
  },
  mutation: {
    UPDATE_USER(state, payload) {
      state.user = payload
    }
  }
}

this.$fetch

You can also manually trigger it:

export default {
  data: () => ({username: 'egoist', user: {}}),
  fetch: {
    user: vm => `/api/user/${vm.username}`
  }
  watch: {
    username() {
      this.$fetch('user')
    }
  }
}

this.$http

Access axios directly, which means you can do this.$http.get/post and such in component.

state and value

It's just like Promise

  • pending: initial state, not fulfilled or rejected.
  • fulfilled: meaning that the operation completed successfully.
  • rejected: meaning that the operation failed.
  • value: the data which is fetched by the request
  • reason: the reason(Error) why the request failed

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

vue-fetch-data © egoist, Released under the MIT License.
Authored and maintained by egoist with help from contributors (list).

egoistian.com · GitHub @egoist · Twitter @_egoistlily

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].