vue学习笔记(8):v-resource

1.vue-resource

getAllList(){

this.$http.get('请求地址').then(result=>{

var result = result.body

if(result.status===0){

//成功

this.list = result.message;

}else{

alert("获取数据失败")

}

})

}

写在created生命周期里,来调用

created(){

this.getAllList()

}

2.添加数据到后台服务器

//this.$http.post()中接受3个参数

第一个参数:请求的地址

第二个参数:要提交给服务器的数据,要以对象形式提交给服务器{name:this.name}

第三个参数:是一个配置对象,要以哪种表单数据类型提交过去,{emulateJSON:true},以普通表单格式,将数据提交给服务器 application/x-www-form-urlencoded

3.add(){

this.$http.post('请求地址',{name:this.name},{emulateJSON:true}).then(result=>{

if(result.body.status==0){

//添加成功后只需手动,在调用一下getAllList就能刷新品牌列表了

this.getAllList()

//清空name

this.name=' '

}else{

alert("请求失败")

}

})

}

4.<a href=" " @click.prevent="del(item.id)"></a>

del(id){

this.$http.get("请求地址"+id).then(result=>{

if(result.body.status==0){

this.getAllList()

}else{

}

})

}

5.全局配置数据接口的根域名

<script>

//如果我们通过全局配置了,请求的数据接口根域名,则,在每次单独发起http请求的时候,请求的url路径,应该以相对路径开头,前面不能带/,否则不会启用根路径做拼接

Vue.http.option.root='http://vue.studyit.io'

//this.$http.get('http://vue.studyit.io/api/getprodlist').then(result=>{

})

this.$http.get('api/getprodlist').then(result=>{


})

</script>

6.全局配置emulateJSON

Vue.http.options.emulateJSON=true;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ## 框架和库的区别?> 框架(framework):一套完整的软件设计架构和**解决方案**。> > 库(lib...
    Rui_bdad阅读 3,036评论 1 4
  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 14,029评论 0 38
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,068评论 0 2
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,554评论 0 13
  • 今晚神奇失眠,实在不符合我的睡眠习惯,大概是同学群里聊天戳到我哪根神经了吧。本来关网睡觉,似睡非睡又醒来,发现...
    梅子Mey阅读 466评论 0 0