vue-condition-watcher
Vue Composition API for automatic fetch data when condition has been changed
requires Node.js 12.0.0 or higher.
Features
✔ Auto fetch data when conditions changed.
✔ Auto filter falsy value in conditions.
✔ Auto converts the corresponding type. (string, number, array, date)
✔ Store the conditions within the URL hash every time a condition is changed
✔ Sync the state with the query string and initialize off of that and that back/forward/execute work.
✔ Keep requests first in — first out.
✔ Works for Vue 2 & 3 by the power of vue-demi
? Download Vue3 example here (Use Vite)
? Download Vue2 @vue/composition-api example here
? Online demo with vue-infinite-scroll
Quick Start
Simple example for vue-next
and vue-router-next
Usage
In your project
Or with npm
CDN
Basic Usage
Execute Fetch
conditions
is reactive proxy, easy execute fetch when conditions
value changed
Just call execute
function to send a request if you need.
Update conditions one time.
Conditions Change Event
onConditionsChange
can help you handle conditions changed.
Will return new value and old value.
Fetch Event
The onFetchResponse
, onFetchError
and onFetchFinally
will fire on fetch request.
Prevent Request
Setting the immediate
to false will prevent the request until the execute
function called or conditions changed.
Intercepting Request
The beforeFetch
let you modify conditions before fetch, or you can call cancel
function to stop fetch.
The afterFetch
can intercept the response before data updated
The onFetchError
can intercept the response before data and error updated
More Configs
-
config
: An object of config for vue-condition-watcher-
fetcher
(⚠️Required) : Can be any asynchronous function to fetch data -
conditions
(⚠️Required) : An object of conditions, also be initial value -
defaultParams
: An object of fetcher's default
parameters -
initialData
:data
default value is null, and you can settingdata
default value by use this config -
immediate
: Setting theimmediate
to false will prevent the request until theexecute
function called.immediate
default istrue
.
-
-
queryOptions
: An object of options to sync query string with conditions-
⚠️
queryOptions
work base on vue-router, you need install vue-router first. -
sync
: key of provide name ( String | Symbol )- main.js: register router
- then
-
ignore
: you can ignore key name from conditions, will not push with query. -
navigation
: use vue router navigation method push or replace, default value is push.
-
How to use in vue@2 with @vue/composition-api
-
( Good ) Add
provide
inmain.js
-
Add
provide
in current file
How to use in Nuxt with @nuxtjs/composition-api
-
Add
provide
in current file
Return Values
conditions
: An object and returns a reactive proxy of conditionsdata
: Data resolved byconfig.fetcher
error
: Error thrown byconfig.fetcher
loading
: Request is fetchingexecute
: The function to fetch dataresetConditions
: Reset conditions to initial valueonConditionsChange
: Will fire on conditions changedonFetchSuccess
: Will fire on fetch request successonFetchError
: Will fire on fetch request erroronFetchFinally
: Will fire on fetch finished
Lifecycle

-
onConditionsChange
Fire new conditions value and old conditions value.
-
beforeFetch
You can modify conditions before fetch, or you can call second of arguments to stop fetch this time.
-
afterFetch
&onFetchSuccess
afterFetch
fire beforeonFetchSuccess
afterFetch
can modify data before update.Type Modify data before update afterFetch config ⭕️ onFetchSuccess event ❌ -
onFetchError(config)
&onFetchError(event)
config.onFetchError
fire beforeevent.onFetchError
config.onFetchError
can modify data and error before update.Type Modify data before update Modify error before update onFetchError config ⭕️ ⭕️ onFetchError event ❌ ❌ -
onFetchFinally
Will fire on fetch finished.