3

Inside this page (https://nuxtjs.org/api/configuration-loading-indicator#custom-indicators) says that I can create a custom loading indicator but not says how.

Somebody can help me - how to create and set this into to nuxt.config?

user41495
  • 35
  • 1
  • 1
  • 4

6 Answers6

6

Here is a collection of default loading indicators in Nuxt.js source code.

Basically, you can specify the HTML template to use as a loadingIndicator in nuxt.config.js.

export default {
  ..., // Other Nuxt configuration

  // Simple usage:
  loadingIndicator: '~/custom-locading-indicator.html',

  // Or with dynamic configuration variables passed via lodash template syntax
  loadingIndicator: {
    name: '~/custom-locading-indicator.html',
    color: '#000',
    background: '#fff'
  }
}

Note, that indicators can have access to

aBiscuit
  • 4,414
  • 1
  • 17
  • 28
  • it's another loading. I need for https://nuxtjs.org/api/configuration-loading-indicator not for https://nuxtjs.org/api/configuration-loading – user41495 Nov 07 '18 at 18:19
  • The info that I wanted is for: Custom Loading Indicator – user41495 Nov 07 '18 at 18:22
  • How can I use an image as the loader in this html template? @aBiscuit – MontyTheMack Jan 04 '19 at 21:47
  • The same way you use in any HTML file - with an image tag (or background) and a link to the image. And place the image inside `static` folder. – aBiscuit Jan 04 '19 at 21:54
  • 1
    in my nuxt.config.js loadingIndicator: { name: '/loadingIndicator.html', }, and in static folder i have made on loadingIndicator.html file but it didn't work. the loader is not visible. but when i use collection of nuxt it work. custom didn't work. – Amar Ratna Shakya Feb 15 '19 at 05:02
  • As you can see in examples above, we use webpack alias for `name` property of `loadingIndicator`. This means, that path to html file is resolved during build process. At this point, I would not recommend putting indicator template file into a `static` static folder, as it served a different purpose. Instead, you can put it in `components` or a custom folder and provide a correct path. Example with `components` folder: `name: '~/components/loading-indicator.html'`. Also, suggest not to use camel case for html filenames. – aBiscuit Feb 15 '19 at 10:08
1

Here is what you need :

You can create a component for your app. You can read about the Nuxt Loading Here

  • Set the component has loading: <your-component-path> Read more here The loading indicator Property.
  • For nuxt to load it on your pages, you need to add :
  loadingIndicator: {
    name: 'chasing-dots',
    color: 'purple',
    background: 'green'
  }

Here is an example of how I configured the component in my app.

export default {

  // LoadingBar component
  loading: '~/path-to-your-loading-component/Loading.vue',
}

On the page you want to load, add this.

export default {
  /*
   ** programmatically start the loader so we force the page to take x2seconds to load
   */
  mounted() {
    this.$nextTick(() => {
      this.$nuxt.$loading.start()
      setTimeout(() => this.$nuxt.$loading.finish(), 2000)
    })
  }
}
</script>
```
damisparks
  • 93
  • 1
  • 10
0

Nuxt also provides a $loading component so you can use it anywhere in your app, anyway you want to. Using $nuxt.$loading.get() will return the current loading percentage.

For example:

<template>
  <div>
    <h1>Home page</h1>
    <div v-show="$nuxt.$loading.get() > 0">
      {{loadingIndicator}}%
    </div>
  </div>
</template>

<script>
export default {
  computed: {
    loadingIndicator() {
      return this.$root.$loading.get()
    }
  }
}
</script>
femanzo
  • 179
  • 1
  • 7
0

following @femanso I managed to customise my loading component with window.$nuxt.$root.$loading.percent instead of $nuxt.$loading.get()

computed: {
    loadingIndicator() {
      return  window.$nuxt.$root.$loading.percent
    }
  },
0

If someone stumbles into this and wants to get the current loading progress from a vue component, you can use this:

EDIT: It worked until I restarted the dev server, then it stopped working. So not sure if this is a viable solution.

  computed: {
    nuxtLoading() {
      return this.$nuxt.$loading.percent
    },
  },
0

Using Bootstrap 5 spinner (https://v5.getbootstrap.com/docs/5.0/components/spinners/), mode: 'universal'

In nuxt.config.js

loading: '~/components/loading.vue'

in components

<template>
  <div 
    v-if="isLoading"
    class="loader"
  >
    <div class="loader__spinner spinner-border text-primary" role="status">
      <span class="sr-only">Loading...</span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isLoading: false
    }
  },
  methods: {
    start() {
      this.isLoading = true
    },
    finish() {
      this.isLoading = false
    }
  }
}
</script>

<style lang="scss" scoped>
.loader {
  &__spinner {
    position: fixed;
    top: 10px;
    left: 10px;
  }
}
</style>

https://nuxtjs.org/guides/configuration-glossary/configuration-loading https://codesandbox.io/s/github/nuxt/nuxt.js/tree/dev/examples/custom-loading?from-embed=&file=/components/loading.vue

atazmin
  • 4,757
  • 1
  • 32
  • 23