本文摘自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前端:
阅读剩余部分
相关阅读 >>
bootstrap与vue区别
vue.js支持ie9吗
vue使用import()提示语法错误怎么办
vue中computed和watch的区别是什么?
一起看看v-for中key属性的作用!
vue组件中如何引入css文件
.vue文件里怎么写scss?
vue $on是什么意思
vue中v-if和v-show的区别是什么?
vue+webpack2实现路由懒加载的方法介绍
更多相关阅读请进入《vue》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » 怎么用vue.js做异步请求