You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
vue-wait helps to manage multiple loading states on the page without any conflict. It's based on a very simple idea that manages an array (or Vuex store optionally) with multiple loading states. The built-in loader component listens its registered loader and immediately become loading state.
Quick Start
If you are a try and learn developer, you can start trying the vue-wait now using codesandbox.io.
1. Install:
yarn add vue-wait
2. Require:
For Vue 2.x
importVueWaitfrom'vue-wait'
Vue.use(VueWait)
newVue({ // your vue config wait: newVueWait(), })
createApp(App)// Create app with root component .use(VueWait)// Register vue-wait .mount('#app')
3. Use in Your Components
"><template> <v-waitfor="my list is to load"> <template slot="waiting"> <div> <imgsrc="loading.gif" /> Loading the list... div> template> <ul> <liv-for="item in myList">{{ item }}li> ul> v-wait> template>
<script> exportdefault { data() { return { myList: [] } }, asynccreated() { // start waiting this.$wait.start('my list is to load');
this.myList=awaitfetch('/my-list-url');
// stop waiting this.$wait.end('my list is to load'); }, }; script>
vue-wait has more abilities to make the management easier, please read the complete documentation.
$ yarn add vue-wait # or if you using npm $ npm install vue-wait
via Vue UI:
Usage
importVueWaitfrom'vue-wait'
Vue.use(VueWait)// add VueWait as Vue plugin
Then you should register wait property (VueWait instance) to the Vue instance:
component name, you can set `my-loader` etc.
registerDirective: true, // Registers `v-wait` directive
directiveName: 'wait', // directive name, you can set `my-loader` etc.
}),
});">newVue({ el: '#app', store, wait: newVueWait({ // Defaults values are following: useVuex: false,// Uses Vuex to manage wait state vuexModuleName: 'wait',// Vuex module name
registerComponent: true,// Registers `v-wait` component componentName: 'v-wait',// component name, you can set `my-loader` etc.
registerDirective: true,// Registers `v-wait` directive directiveName: 'wait',// directive name, you can set `my-loader` etc.
}), });
Usage with Vuex
Simply set useVuex parameter to true and optionally override
vuexModuleName
importVueWaitfrom'vue-wait'
Vue.use(Vuex) Vue.use(VueWait)// add VueWait as Vue plugin
Then you should register VueWait module:
newVue({ el: '#app', store, wait: newVueWait({ useVuex: true,// You must pass this option `true` to use Vuex vuexModuleName: 'vuex-example-module'// It's optional, `wait` by default. }), });
Now VueWait will use Vuex store for data management which can be traced in Vue DevTools > Vuex
Usage with Nuxt.js
Add vue-wait/nuxt to modules section of nuxt.config.js
{ modules: [ // Simple usage 'vue-wait/nuxt'
// Optionally passing options in module configuration ['vue-wait/nuxt',{useVuex: true}] ],
// Optionally passing options in module top level configuration wait: {useVuex: true} }
VueWait Options
You can use this options for customize VueWait behavior.
Option Name
Type
Default
Description
accessorName
String
"$wait"
You can change this value to rename the accessor. E.g. if you rename this to $w, your VueWait methods will be accessible by $w.waits(..) etc.
useVuex
Boolean
false
Use this value for enabling integration with Vuex store. When this value is true VueWait will store data in Vuex store and all changes to this data will be made by dispatching actions to store
vuexModuleName
String
"wait"
Name for Vuex store if useVuex set to true, otherwise not used.
registerComponent
Boolean
true
Registers v-wait component.
componentName
String
"v-wait"
Changes v-wait component name.
registerDirective
Boolean
true
Registers v-wait directive.
directiveName
String
"v-wait"
Changes v-wait directive name.
Global Template Helpers
vue-wait provides some helpers to you to use in your templates.
All features can be obtained from $wait property in Vue components.
.any
Returns boolean value if any loader exists in page.
End loader
"><template> <buttonv-wait:click.end='"create user"'>End loaderbutton> template>
v-wait:toggle='"loader name"'
Toggles given loader on click.
Toggles the loader
"><template> <buttonv-wait:toggle='"flip flop"'>Toggles the loaderbutton> template>
v-wait:click.progress='["loader name", 80]'
Sets the progress of given loader on click.
Set the "downloading" loader to 80
"><template> <buttonv-wait:click.progress='["downloading", 80]'>Set the "downloading" loader to 80button> template>
Loading Action and Getter Mappers
vue-wait provides mapWaitingActions and mapWaitingGetters mapper to be used with your Vuex stores.
Let's assume you have a store and async actions called createUser and updateUser.
It will call the methods you map and will start loaders while action is resolved.
waitFor(loader String, func Function [,forceSync = false])
Decorator that wraps function, will trigger a loading and will end loader after the original function (func argument) is finished.
By default waitFor return async function, if you want to wrap default sync function pass true in last argument
Example using with async function
setTimeout(resolve, ms));
}
// do work here
await sleep(3000);
// simulate some api call
this.fetchResponse = Math.random()
})
}
...">import{waitFor}from'vue-wait';
... methods: { fetchDataFromApi: waitFor('fetch data',asyncfunction(){ functionsleep(ms){ returnnewPromise(resolve=>setTimeout(resolve,ms)); } // do work here awaitsleep(3000); // simulate some api call this.fetchResponse=Math.random() }) } ...
See also examples/wrap-example
Using v-wait Component
If you disable registerComponent option then import and add v-wait into components
In template, you should wrap your content with v-wait component to show loading on it.
This will be shown when "fetching data" loader starts.
This will be shown when "fetching data" loader ends.
"><v-wait for='fetching data'>
This will be shown when "fetching data" loader starts.
This will be shown when "fetching data" loader ends. v-wait>
Better example for a button with loading state:
Creating User...
Create User
"><button :disabled='$wait.is("creating user")'> Creating User... Create User
button>
Transitions
You can use transitions with v-wait component.
Just pass props and listeners to the v-wait with transition prop.
Loading...
My content
"><v-wait for="users" transition="fade" mode="out-in" :duration="1000" enter-active-class="enter-active" @leave='someAwesomeFinish()' >
Loading...
My content v-wait>
Making Reusable Loader Components
With reusable loader components, you will be able to use custom loader components as example below. This will allow you to create better user loading experience.
In this example above, the tab gets data from back-end, and the table loads data from back-end at the same time. With vue-wait, you will be able to manage these two seperated loading processes easily:
Now you can use your spinner everywhere using slot='waiting' attribute:
<template lang="pug"> <v-waitfor="fetching data"> <my-waiterslot="waiting" /> <div> <p>My main content after fetching data...p> div> v-wait> template>
Using with external spinner libraries
You can use vue-wait with another spinner libraries like epic-spinners or other libraries. You just need to add slot="waiting" to the component and Vue handles rest of the work.