代码:

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="./lib/vue-2.4.0.js"></script>
<script src="./lib/vue-resource-1.3.4.js"></script>
<link rel="stylesheet" href="./lib/bootstrap-3.3.7.css">
</head> <body>
<div id="app">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">添加品牌</h3>
</div>
<div class="panel-body form-inline">
<label>
Name:
<input type="text" v-model="name" class="form-control">
</label>
<input type="button" value="添加" @click="add" class="btn btn-primary">
</div>
</div> <table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Ctime</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<tr v-for="item in list" :key="item.id">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>{{item.ctime}}</td>
<td>
<a href="" @click.prevent="del(item.id)">删除</a>
</td>
</tr>
</tbody>
</table>
</div> <script>
// 如果我们通过全局配置了,请求的数据接口根域名,则在每次单独发起 http请求的时候,请求的 url 路径应该以相对路径开头,
//$http.get('api/getprodlist')前面不能带/,否则不会启用根路径做拼接;
Vue.http.options.root = 'http://vue.studyit.io/'; // 全局启用 emulateJSON 选项
Vue.http.options.emulateJSON = true; // 创建 Vue 实例,得到 ViewModel
var vm = new Vue({
el: '#app',
data: {
name: '',
list: [ // 存放所有品牌列表的数组
]
},
created() { // 当 vm 实例的 data 和 methods 初始化完毕后,vm实例会自动执行created 这个生命周期函数
this.getAllList()
},
methods: {
getAllList() { // 获取所有的品牌列表
// 分析:
// 1. 由于已经导入了 Vue-resource这个包,所以可直接通过 this.$http 来发起数据请求
// 2. 根据接口API文档知道,获取列表的时候,应该发起一个 get 请求
// 3. this.$http.get('url').then(function(result){})
// 4. 当通过 then 指定回调函数之后,在回调函数中,可以拿到数据服务器返回的 result
// 5. 先判断 result.status 是否等于0,如果等于0,就成功了,可以 把 result.message 赋值给 this.list ; 如果不等于0,可以弹框提醒,获取数据失败! this.$http.get('api/getprodlist').then(result => {
// 注意:通过 $http 获取到的数据都在 result.body 中放着
var result = result.body
if (result.status === 0) {
// 成功了
this.list = result.message
} else {
// 失败了
alert('获取数据失败!')
}
})
},
//2.添加品牌列表到后台服务器
add() {
// 分析:
// 1. 通过查看数据API接口发现,要发送一个 Post 请求,this.$http.post
// 2. this.$http.post() 中接收三个参数:
// 2.1 第一个参数: 要请求的URL地址
// 2.2 第二个参数: 要提交给服务器的数据 ,要以对象形式提交给服务器 { name: this.name }
// 3.3 第三个参数: 是一个配置对象,要以哪种表单数据类型提交过去, { emulateJSON: true }, 以普通表单格式,将数据提交给服务器 application/x-www-form-urlencoded
// 3.在 post方法中,使用 .then 来设置成功的回调函数,如果想要拿到成功的结果,需要result.body /* this.$http.post('api/addproduct', }{ name: this.name , { emulateJSON: true }).then(result => {
if (result.body.status === 0) {
// 成功了!
// 添加完成后,只需要手动,再调用一下 getAllList 就能刷新品牌列表了
this.getAllList()
// 清空 name
this.name = ''
} else {
// 失败了
alert('添加失败!')
}
}) */ //全局启用了emulateJSON 选项
this.$http.post('api/addproduct', { name: this.name }).then(result => {
if (result.body.status === 0) {
// 成功了!
// 添加完成后,只需要手动,再调用一下 getAllList 就能刷新品牌列表了
this.getAllList()
// 清空 name
this.name = ''
} else {
// 失败了
alert('添加失败!')
}
})
},
//3.删除品牌
del(id) {
this.$http.get('api/delproduct/' + id).then(result => {
if (result.body.status === 0) {
// 删除成功
this.getAllList()
} else {
alert('删除失败!')
}
})
}
}
});
</script>
</body>
</html>

最新文章

  1. ListView之多种类型Item
  2. yii2 随笔
  3. angular $http 与form表单的select--&gt;refine
  4. Struts2运行流程
  5. HTML5-布局的使用
  6. android框架整理
  7. [Windows-Linux]Windows and Linux 共享文件
  8. java UncaughtExceptionHandler 处理线程意外中止
  9. canvas 动态飞速旋转的矩形
  10. shark错误:Query returned non-zero code: -101
  11. 一个方便的shell命令,查看软件安装目录
  12. oracle 语句
  13. Linux下忘记rootpassword怎么办?
  14. IOS中UIDatePicker
  15. 机器学习笔记3-Tensorflow简介
  16. MPP-编码示例
  17. [LeetCode] 5. 最长回文子串
  18. 虚拟机设置固定ip可以使shell远程连接到服务器
  19. 随手记-egg入门
  20. Spring Boot(1)——开发你的第一款Spring Boot应用(Edition1)

热门文章

  1. 创建.Net Core For WPF项目并且添加VS Code编译运行支持
  2. 【miscellaneous】【ARM-Linux开发】ARM平台基于嵌入式Linux Gstreamer 使用
  3. Npcap.例子(raw tcp syn)
  4. CenOS 7 安装JDK
  5. for i in range()
  6. K8S从入门到放弃系列-(7)kubernetes集群之kube-scheduler部署
  7. mssql sqlserver时间戳与时间格式互相转换的方法分享
  8. 腾讯云+阿里云 搭建hadoop + hbase
  9. 【51nod】2622 围绕着我们的圆环
  10. TypeScript 枚举