vue mysql添加 删除_Vue之通过连接数据库的接口获取列表实现添加删除功能

把最近学习vue的一些知识点记录下来,今天记录一下Vue通过vue-resource连接数据库接口渲染列表和添加删除功能

首先我们得引入vue的版本文件和vue-resource.js,注意:vue-resource的引入要在vue版本文件之后

然后把列表页面写好,然后在methods里写一个获取列表数据的方法:

getAllList() { //获取所有的品牌列表

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

var result = result.body

if (result.status === 0) {

this.list = result.message

} else {

alert(‘获取数据失败‘)

}

})

}

当然list是在data里面已经定义好的

data: {

name:‘‘,

list: [

{ id:1, name: ‘五菱宏光‘, ctime: newDate() },

{ id:2, name: ‘摩托罗拉‘, ctime: newDate() }

]

}

然后调用vue的生命周期函数里面的created()函数,调用此函数时当vm实例的data和methods初始化完毕后,vm实例会自动执行,在此函数里面调用刚刚写的getAllList()函数。此时列表就能获取到。添加和删除功能类似,需要注意的是删除的时候需要传入对应数据的id值,我们可以在url后面直接拼接。为了方便接口地址的修改操作,可以通过全局配置,请求数据接口的根域名和全局启用emulateJSON选项

Vue.http.options.root=‘http://www.liulongbin.top:3005/‘;

Vue.http.options.emulateJSON=true;

以下是全部代码:

Document

添加品牌

Name:

IdNameCtimeOperation

{{ item.id}}{{ item.name}}{{ item.ctime}}

删除

//通过全局配置,请求数据接口的根域名

Vue.http.options.root=‘http://www.liulongbin.top:3005/‘;//全局启用emulateJSON选项

Vue.http.options.emulateJSON=true;var vm = newVue({

el:‘#app‘,

data: {

name:‘‘,

list: [

{ id:1, name: ‘五菱宏光‘, ctime: newDate() },

{ id:2, name: ‘摩托罗拉‘, ctime: newDate() }

]

},

created() {//当vm实例的data和methods初始化完毕后,vm实例会自动执行

this.getAllList()

},

methods: {

add() {//添加

this.$http.post(‘api/addproduct‘, { name: this.name }, { emulateJSON: true }).then(result =>{if (result.body.status === 0) {//成功

this.getAllList()this.name=‘‘}else{

alert(‘获取数据失败‘)

}

})

},

getAllList() {//获取所有的品牌列表

this.$http.get(‘api/getprodlist‘).then(result =>{var result =result.bodyif (result.status === 0) {this.list =result.message

}else{

alert(‘获取数据失败‘)

}

})

},

del(id){//删除

this.$http.get(‘api/delproduct/‘+id).then(result=>{if (result.body.status === 0) {//成功

this.getAllList()

}else{

alert(‘获取数据失败‘)

}

})

}

}

})

使用是记得引入对应的文件。

原文:https://www.cnblogs.com/Yaucheun/p/10823991.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值