axios拦截器
数据请求前拦截
数据请求得到结果后拦截
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<style>
p{
display: none;
}
.show{
display: block;
}
.hidden{
display: none;
}
</style>
</head>
<body>
<div id="app">
<button @click = "getData"> getData </button>
<p> 正在加载... </p>
<ul>
<li v-for = "movie in movies" :key = "movie.id">
{{ movie.name }}
</li>
</ul>
</div>
</body>
<script src="../../lib/vue.js"></script>
<script>
axios.interceptors.request.use(function (config) {
document.querySelector('p').className = "show"
return config;
}, function (error) {
return Promise.reject(error);
});
axios.interceptors.response.use(function (response) {
document.querySelector('p').className = ''
return response;
}, function (error) {
return Promise.reject(error);
});
new Vue({
el: '#app',
data: {
movies: null
},
methods: {
getData () {
axios({
url:"https://m.maizuo.com/gateway?cityId=110100&pageNum=1&pageSize=10&type=1&k=1384809",
headers:{
'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.0.4","e":"154277371928424093566579"}',
'X-Host': 'mall.film-ticket.film.list'
}
}).then(res=>{
console.log('res',res)
this.movies = res.data.data.films
})
}
}
})
</script>