1.0.0 • Published 6 years ago

vue-async-helper v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

Vue Async Helper

npm npm

Big thanx to @LinusBorg.

Installation

npm install vue-async-helper --save

Usage

  • assign the function to the window so you can use it everywhere without re-importing

    import VAC from 'vue-async-helper'
    window.VueAsyncComponent = VAC
  • now replace the () => import(...) call with VueAsyncComponent(import(...))

    /* global */
    Vue.component('MyComp', () => import('abc.vue'))                // before
    Vue.component('MyComp', VueAsyncComponent(import('abc.vue')))   // after
    
    /* local */
    components: {
        MyComp: () => import('abc.vue')                // before
        MyComp: VueAsyncComponent(import('abc.vue'))   // after
    },
    propdefaultdescription
    loading"Please Stand By..."A component to use while the async component is loading
    error"Something Went Wrong, Plz Try Again"A component to use if the load fails
    delay200Delay before showing the loading component "in ms"
    timeout5000The error component will be displayed if a timeout is provided and exceeded "in ms"

Changing the helper defaults

  • you can change the default options per call like

    Vue.component('MyComp', VueAsyncComponent(import('abc.vue'), {
        timeout: 1000
    }))
  • or globally

    • first create a new file with the below

      // vac.js
      import VAC from 'vue-async-helper'
      window.VueAsyncComponent = (item) => {
          return VAC(item, {
              loading: LoadingComp,
              error: ErrorComp,
              delay: 500,
              timeout: 2000
          })
      }
    • next require that file and follow the usage as normal

      // app,js
      require('vac.js')
      
      Vue.component('MyComp', VueAsyncComponent(import('abc.vue')))