划重点
- 使用vue-resource.js库 进行网络请求操作
- POST : this.$http.post ( … )
- GET : this.$http.get ( … )
小鸡炖蘑菇
<!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">
<script src="./lib/vue-2.6.10.js"></script>
<!-- <script src="https://cdn.jsdelivr.net/npm/vue-resource@1.5.1"></script> -->
<script src="./lib/vue-resource.js"></script>
<title>Document</title>
</head>
<body>
<div id="app">
<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-show="flag" v-for="item in list" :key="item.id" >
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.ctime }}</td>
<td>
<a href="http://www.baidu.com" @click.prevent="del">删除</a>
</td>
</tr>
</tbody>
</table>
</div>
<script>
Vue.http.options.root = 'http://jsonplaceholder.typicode.com/';//全局配置根 地址
Vue.http.options.emulateJSON = true;
var vm = new Vue({
el: '#app',
data: {
flag: false,//默认没有加载完成数据的情况下:不显示
list: [
{ id: 1, name: '奔驰', ctime: new Date() },
{ id: 2, name: '法拉利', ctime: new Date() }
]
},
created() {
this.testUrl();
},
methods: {
del() {
alert("22344578");
},
testUrl() {
// 前端服务器测试用的万能地址:http://jsonplaceholder.typicode.com/users
//POST
// this.$http.post('http://jsonplaceholder.typicode.com/users').then(response => {
// alert('Post传统请求成功');
// }, response => {
// alert('Post请求失败~~~');
// })
// GET /someUrl
this.$http.get('users').then(response => {
this.flag = true;
// get body data
this.list = response.body;
alert(this.list+"333")
});
// 传统写法
// this.$http.get('http://jsonplaceholder.typicode.com/users').then(function (response) {
// alert("传统请求成功");
// // 响应成功回调
// }, function (response) {
// // 响应错误回调
// alert('传统请求失败~~~');
// });
}
}
})
</script>
</body>
</html>