1、安装 vue-resource插件,将http请求注入vue中
cnpm install vue-resource
2、安装完成后,在组件需要请求服务器的位置,一般是在组件创建成功后进行请求,即(通过$http进行请求):
export default{
created:function(){//组件创建成功后请求
this.$http.get('getList',{id:1})
.then(function(data){//请求成功的返回值
},function(err){//请求失败后的返回值
});
}
}
3、安装json-server,模拟服务器请求
cnpm install json-server --save
4、在配置文件中设置代理:
// config/index.json
dev{
proxyTable: {
'/api/':'http://localhost:8082/'
},
}
///build/webpack.dev.conf.js
apiServer.use('/api',apiRouter);