|
1 | 1 | # vuex-loading
|
2 |
| -简化vuex loading state的维护 |
| 2 | +简化vuex中异步请求,不得不维护loading state的问题,这不是一个loading ui插件,仅仅只是一个简化。 |
3 | 3 |
|
4 | 4 | ## Installing
|
5 | 5 |
|
6 | 6 | Using npm:
|
7 | 7 |
|
8 | 8 | ```bash
|
9 |
| -$ npm install vuex-loading -s |
| 9 | +$ npm install vuex-loadings -s |
| 10 | +``` |
| 11 | + |
| 12 | +## Example |
| 13 | + |
| 14 | + |
| 15 | +```js |
| 16 | +//product.js |
| 17 | +import vxl from 'vuex-loadings' |
| 18 | +const api = { |
| 19 | + listProduct (cb,errorCb) { |
| 20 | + fetch('/api/listProduct').then((result) => { |
| 21 | + cb(result) |
| 22 | + }).catch(error => { |
| 23 | + errorCb(error) |
| 24 | + }) |
| 25 | + } |
| 26 | +} |
| 27 | +const state = { |
| 28 | + products: [] |
| 29 | +} |
| 30 | + |
| 31 | +const getters = { |
| 32 | + products (state) { |
| 33 | + return state.products |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +const actions = { |
| 38 | + //aopLoading会代理api.listProduct,然后返回一个被代理的api.listProduct |
| 39 | + listProduct ({{commit}}) { |
| 40 | + let request = vxl.aopLoading(commit, 'productsLoading',api.listProduct) |
| 41 | + //代理函数调用的时候,会先执行commit('productsLoading',true),在异步请求执行完成后,会在成功回调和失败回调调用前执行commit('productsLoading',false) |
| 42 | + request((result) => { |
| 43 | + commit('setProducts', result) |
| 44 | + }, error => { |
| 45 | + |
| 46 | + }) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +const mutations = { |
| 51 | + setProducts (state, item) { |
| 52 | + state.products = item |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +//使用vxl.mixin将productsLoading相关操作注入state,getters,mutations中,这样我们不需要手写大堆的代码 |
| 57 | +vxl.mixin({state,getters,mutations}, ['products']) |
| 58 | +//或者可以指定loading名字 |
| 59 | +vxl.mixin({state,getters,mutations}, [{'products':'productsLoading'}]) |
| 60 | + |
| 61 | +export { |
| 62 | + state,getters,actions,mutations |
| 63 | +} |
| 64 | +``` |
| 65 | +### 另一种方式 |
| 66 | + |
| 67 | +```js |
| 68 | +import {aopLoading, mapLoadingMutations, mapLoadingState, mapLoadingGetters} from '../vuex-loading' |
| 69 | +const api = { |
| 70 | + listProduct (cb,errorCb) { |
| 71 | + fetch('/api/listProduct').then((result) => { |
| 72 | + cb(result) |
| 73 | + }).catch(error => { |
| 74 | + errorCb(error) |
| 75 | + }) |
| 76 | + } |
| 77 | +} |
| 78 | +const state = { |
| 79 | + //为clues绑定loading,这种方法不支持自定义loading name,默认规则是clues -> cluesLoading |
| 80 | + ...mapLoadingState({ |
| 81 | + clues: {}, |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +const getters = { |
| 87 | + clues(state) { |
| 88 | + return state.clues |
| 89 | + }, |
| 90 | + ...mapLoadingGetters(['cluesLoading']) |
| 91 | + //或者是自定义getters名 |
| 92 | + ...mapLoadingGetters({'cluesLoading': 'getCluesLoading'}) |
| 93 | +} |
| 94 | + |
| 95 | +const actions = { |
| 96 | + listClue({commit}) { |
| 97 | + //同上 |
| 98 | + let request = aopLoading(commit, 'cluesLoading', marketApi.listClue) |
| 99 | + request(items => { |
| 100 | + commit('setClues', {items}) |
| 101 | + }, |
| 102 | + error => { |
| 103 | + }) |
| 104 | + }, |
| 105 | +} |
| 106 | + |
| 107 | +const mutations = { |
| 108 | + setClues(state, {items}) { |
| 109 | + state.clues = items |
| 110 | + }, |
| 111 | + //绑定mutations方法 |
| 112 | + ...mapLoadingMutations(['cluesLoading']) |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +export default { |
| 117 | + namespaced: true, |
| 118 | + state, |
| 119 | + getters, |
| 120 | + actions, |
| 121 | + mutations |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +```vue |
| 126 | +//product.vue |
| 127 | +<template> |
| 128 | + <div>{{productsLoading}}</div> |
| 129 | +</template> |
| 130 | +import {mapGetters} from 'vuex' |
| 131 | +<script> |
| 132 | + export default { |
| 133 | + computed: { |
| 134 | + ...mapGetters ('product', ['productsLoading']) |
| 135 | + } |
| 136 | + data () { |
| 137 | + return { |
| 138 | + |
| 139 | + } |
| 140 | + }, |
| 141 | + |
| 142 | + } |
| 143 | +</script> |
| 144 | +
|
| 145 | +``` |
10 | 146 |
|
0 commit comments