本文摘自PHP中文网,作者藏色散人,侵删。
用vue.js做异步请求的方法:首先在项目中安装axiox;然后在main.js中引入axiox,以供全局使用;接着进行axios get请求;最后实现axios post请求即可。

本教程操作环境:windows7系统、vue2.0版本、thinkpad t480电脑。
推荐:《vue教程》
用vue.js做异步请求
一、axios实现异步请求
1.项目中安装axiox
2.在main.js中引入以供全局使用
1 2 3 4 5 | import axios from 'axios'
axios.defaults.baseURL = "http://157.122.54.189:8080/";
Vue.prototype.$http = axios
|
3.axios之get请求
vue前端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <template>
<div>
</div>
</template>
<script>
export default {
methods:{
getData(){
this.$http.get('/getData1')
.then(r => console.log(r))
.catch(err => console.log(err)),
}
}
mounted(){
this.getData()
}
}
</script>
<style scoped>
</style>
node后端:
server.get('/getData1',function(req,res){
res.send({
'msg':'aaa'
})
})
|
请求结果:

4.axios之post请求
Vue前端:
阅读剩余部分
相关阅读 >>
深入研究vue cli3
vue中elementui是什么
浅谈vue中引入jquery的方法
基于vue移动端ui框架有哪些?
vue中muse-ui是什么
angular脏值检测与vue数据劫持的区别是什么
vue中iview是什么?
一招搞定vue常用指令
vue和react是什么框架?
vue中的样式绑定详解
更多相关阅读请进入《vue》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » 怎么用vue.js做异步请求