如何在NUXT插件中显示所有Axios请求的全局加载

发布于 2025-02-12 12:20:18 字数 432 浏览 1 评论 0原文

我想在全球范围内显示所有Axios请求的

加载

定义

    export default function ({ app, $axios, redirect, store }, inject) {
    
    
        $axios.onRequest(config => {
            this.$nuxt.$loading.start()
        })
    
        $axios.onError(error => {
            this.$nuxt.$loading.finish()
        })
    
      
    }

自 强>在这里。

那么如何修改此代码?

I want to display my custom loading on all axios request globally

for that I've made a plugin for axios to call it like this

plugins/axios.js

    export default function ({ app, $axios, redirect, store }, inject) {
    
    
        $axios.onRequest(config => {
            this.$nuxt.$loading.start()
        })
    
        $axios.onError(error => {
            this.$nuxt.$loading.finish()
        })
    
      
    }

but It get error because we don't have this here .

so how can I modify this code ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素年丶 2025-02-19 12:20:18

NUXT加载属性用于跟踪页面加载状态。这是您可以用自定义组件替换的内部组件:

// nuxt.config.js
export default {
  loading: '~/components/YourLoadingComponent.vue'
}

但它与为HTTP请求设置加载状态的功能不同。我推荐您创建一个组件来管理HTTP请求的加载状态,并且可以使用VUEX管理当前加载状态,然后从axios.js.js插件中修改商店。

// axios.js plugin
export default function ({ $axios, store }) {

  $axios.onRequest(config => {
    store.dispatch('setLoadingStatus', true)
  })

  $axios.onError(error => {
    store.dispatch('setLoadingStatus', false)
  })

}

然后,您可以管理请求状态:

// store/index.js file
export const state = () => ({
  isRequestLoading: false
})

export const mutations = {
  saveLoadingStatus(state, payload) {
    state.isRequestLoading = payload
  }
}

export const actions = {
  setLoadingStatus({ commit }, status) {
    commit('saveLoadingStatus', status)
  }
}

并且非常简单的示例组件:

<template>
  <div>
    <p v-if="isRequestLoading">Is Loading</p>
    <p v-else>Request completed</p>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  computed: {
    ...mapState({
      isRequestLoading: (state) => state.isRequestLoading
    })
  }
}
</script>

您可以将加载条或旋转器设置在此组件内的窗口中显示。

The nuxt loading property is used to track pages load status. This is an internal component that you can replace with custom component:

// nuxt.config.js
export default {
  loading: '~/components/YourLoadingComponent.vue'
}

But it is not the same feature that set a loading status for your HTTP requests. I recomend you to create a component to manage the loading status for your HTTP requests, and you can manage the current loading status with Vuex, and then modify the store from your axios.js plugin.

// axios.js plugin
export default function ({ $axios, store }) {

  $axios.onRequest(config => {
    store.dispatch('setLoadingStatus', true)
  })

  $axios.onError(error => {
    store.dispatch('setLoadingStatus', false)
  })

}

Then you Vuex store can manage the request status:

// store/index.js file
export const state = () => ({
  isRequestLoading: false
})

export const mutations = {
  saveLoadingStatus(state, payload) {
    state.isRequestLoading = payload
  }
}

export const actions = {
  setLoadingStatus({ commit }, status) {
    commit('saveLoadingStatus', status)
  }
}

And very simple example component:

<template>
  <div>
    <p v-if="isRequestLoading">Is Loading</p>
    <p v-else>Request completed</p>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  computed: {
    ...mapState({
      isRequestLoading: (state) => state.isRequestLoading
    })
  }
}
</script>

You can set a loading bar or spinner to display in your window inside this component.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文