main。js文件中添加
import axios from 'axios'
Vue.prototype.$http = axios
之后就可以在组件中直接使用this.$http.post与上一篇的axios。port方法一样。
为了修复之前注册的BUG,axios部分代码修改为
this.$http.post('/api/UserManager/adduser?name='+this.name+'&password='+this.pass)
.then((response)=>{//主要就是这一行需要添加=>才能取json中的数据赋给别的变量
console.log(response);
this.result1=response.data.result;
//下面的这部分判断放的位置很重要,因为axios是异步请求,所以放别的位置容易造成异步请求的BUG,无法正确与后台交互。
if(this.result1==0){
this.hint='此用户名已被使用';
}else if(this.result1==1){
this.hint=this.name+'注册成功';
}
})
.catch(function(err){
console.log(err);
});