Nuxt.js 异步请求动画 - 使用 <nuxt-loading> 组件
Nuxt.js 提供了一个非常简单的方法来为异步请求添加动画。您可以使用 <nuxt-loading> 组件来实现这一点。
首先,在您的应用程序中安装 @nuxtjs/loading-screen 模块:
npm install @nuxtjs/loading-screen
然后在 nuxt.config.js 中启用该模块:
modules: [
'@nuxtjs/loading-screen'
],
接下来,您可以在页面中使用 <nuxt-loading> 组件来添加动画。例如:
<template>
<div>
<h1>My Page</h1>
<nuxt-loading :show='loading' />
<div v-if='!loading'>
<!-- Your page content -->
</div>
</div>
</template>
<script>
export default {
data() {
return {
loading: false
}
},
methods: {
async loadData() {
this.loading = true
// Perform async request
await this.$axios.$get('/api/data')
this.loading = false
}
},
mounted() {
this.loadData()
}
}
</script>
在上面的示例中,我们在页面加载时调用了 loadData 方法来执行异步请求。在请求开始时,我们将 loading 属性设置为 true,并在请求完成时将其设置为 false。我们还使用了 <nuxt-loading> 组件来显示动画,只有在 loading 属性为 true 时显示。
原文地址: https://www.cveoy.top/t/topic/lU59 著作权归作者所有。请勿转载和采集!