如何在 Nuxt.js 中创建自定义加载指示器?

How can I create a custom loading indicator in Nuxt.js?

在此页面内 (https://nuxtjs.org/api/configuration-loading-indicator#custom-indicators) 说我可以创建自定义加载指示器,但没有说明如何创建。

有人可以帮助我 - 如何创建并将其设置为 nuxt.config?

这里是Nuxt.js源代码中默认[=​​13=]的集合。

基本上,您可以指定 HTML 模板用作 nuxt.config.js 中的 loadingIndicator

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'
  }
}

请注意,指标可以访问

Nuxt 还提供了一个 $loading 组件,因此您可以随心所欲地在应用程序的任何位置使用它。 使用 $nuxt.$loading.get() 将 return 当前加载百分比。

例如:

<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>

跟随@femanso,我设法使用 window.$nuxt.$root.$loading.percent 而不是 $nuxt.$loading.get()

自定义我的加载组件
computed: {
    loadingIndicator() {
      return  window.$nuxt.$root.$loading.percent
    }
  },

如果有人遇到这个问题,想从一个vue组件中获取当前的加载进度,你可以使用这个:

编辑:它一直有效,直到我重新启动开发服务器,然后它停止工作。所以不确定这是否是一个可行的解决方案。

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

使用 Bootstrap 5 微调器 (https://v5.getbootstrap.com/docs/5.0/components/spinners/),模式:'universal'

在 nuxt.config.js

正在加载:'~/components/loading.vue'

在组件中

<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

这是您需要的:

您可以为您的应用程序创建一个组件。 您可以阅读 Nuxt Loading Here

  • 设置组件有loading: <your-component-path> 在这里阅读更多 The loading indicator Property.
  • 为了让 nuxt 在你的页面上加载它,你需要添加:
  loadingIndicator: {
    name: 'chasing-dots',
    color: 'purple',
    background: 'green'
  }

这是我如何在我的应用程序中配置组件的示例。

export default {

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

在您要加载的页面上,添加此内容。

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>
```