Nuxt.js: Async load component?

Created on 5 Nov 2017  路  6Comments  路  Source: nuxt/nuxt.js

I want load vue-awesome-swiper full asynchronously (include css) for optimize first time load site (load speed). Exclude plugins, modules, because it loading before site loaded. I need it async,later. How i can do this?

// TEMPLATE (pug)
no-ssr
        vue-awesome-swiper
// SCRIPT
  let VueAwesomeSwiper
  if (process.browser) {
    VueAwesomeSwiper = require('vue-awesome-swiper/ssr')
  }

  export default{
    components: {VueAwesomeSwiper}
  }
// STYLE (stylus)
@import "swiper/dist/css/swiper.css"

And how cache it for used on another pages? what problems can there be?

This question is available on Nuxt.js community (#c1801)
help-wanted

Most helpful comment

You can use Async Componenst and Dyanmic Componenst in Vue.js

<template>
  <div>
    <component :is="asyncComp"></component>
    <button @click="clickBtn">Click Me</button>
  </div>
</template>

<script>
export default {
  data: () => ({
    asyncComp: ''
  }),
  components: {
    'example': () => {
      import('@/components/example')
    }
  },
  clickBtn() {
    this.asyncComp = 'example'
  }
}
</script>

All 6 comments

@clarkdo I need a load component after page loaded. On created () or on demand. For example: index page has component slider, whose use vue-awesome-swiper lib. Page loaded without style and js of vue-awesome-swiper, only site css, on slider space used preloader gif. After that, i need load this slider lib with css and js, slider appears on index page. I need this.

You can use Async Componenst and Dyanmic Componenst in Vue.js

<template>
  <div>
    <component :is="asyncComp"></component>
    <button @click="clickBtn">Click Me</button>
  </div>
</template>

<script>
export default {
  data: () => ({
    asyncComp: ''
  }),
  components: {
    'example': () => {
      import('@/components/example')
    }
  },
  clickBtn() {
    this.asyncComp = 'example'
  }
}
</script>

figured out, thanks!

Hi,
@clarkdo it doesn't work for me, I have to return the import, example:

    'post-share': () =>
      import(
        /* webpackChunkName: "PostShare" */
        '@/components/Post/Share/Share.vue'
      )

I can't find documentation about lazyloading, it's weird.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vadimsg picture vadimsg  路  3Comments

shyamchandranmec picture shyamchandranmec  路  3Comments

lazycrazy picture lazycrazy  路  3Comments

danieloprado picture danieloprado  路  3Comments

bimohxh picture bimohxh  路  3Comments