javascript - 视觉 : Display Loader when request is in progress

标签 javascript vue.js vue-router vuex

我想实现如下流程:

当正在进行 http 请求时,显示加载程序。当请求完成时隐藏加载器。

最佳答案

I assume that you want to show a loader when a http request is on progress.

<template>

    <div>

        <div v-if="loading">
            <!-- here put a spinner or whatever you want to indicate that a request is in progress -->
        </div>

        <div v-else>
            <!-- request finished -->
        </div>

    </div>

</template>

<script>
    import axios from 'axios'

    export default {
        data: () => {
          loading: false
        },

        methods: {
          makeRequest () {
            this.loading = true //the loading begin
            axios.get('/example')
            .then(response => { ... }) // code to run on success
            .catch(error => { ... }) // code to run on error
            .finally(() => (this.loading = false)) // set loading to false when request finish
          }
        }
    }
</script>

请注意,我使用的是 axios在示例中,但逻辑适用于其他 htpp 库或 fetch

关于javascript - 视觉 : Display Loader when request is in progress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49356597/

相关文章:

javascript - 如何在加载某些异步数据(在 Vuex 存储中)之前防止任何路由?

javascript - 当它解决了 Vue firebase 时传递 Prop

javascript - Ext JS 中方向改变时的对话框位置改变

javascript - 在 Vuex 中使用 get/set 计算属性和 mapState、mapMutation

Javascript 检查数字值是否为零(而不是 null)或更高但不是负数

vue.js - Vue keep alive 在 $destroy 之后不再缓存

vue.js - Vue-router 组件复用

javascript - Bootstrap 4 抛出错误,未捕获类型错误?

javascript - 用于在 javascript 验证中接受所有类型电话号码的正则表达式

javascript - 如何将 gulp.watch() 传递给管道?