axios拦截器的简单使用

<template>
  <div id="app">
    <h1>axios拦截器</h1>
    <button @click="handleClick">按钮</button>
    <p>{{title}}</p>
    <div v-if="loading">loading...</div>
  </div>
</template>
<script>
import axios from 'axios'
export default {
  data() {
    return {
      title: '',
      loading: false
    }
  },
  methods: {
    handleClick() {
      axios.get('https://jsonplaceholder.typicode.com/todos/1').then((res) => {
        this.title = res.data.title
      })
    }
  },
  created() {
    axios.interceptors.request.use((config) => {
      this.loading = true
      return config
    })
    axios.interceptors.response.use((config) => {
      this.loading = false
      return config
    })
  }
}
</script>

 

上一篇:css 旋转动画


下一篇:手把手带你实现vue全屏loading插件