最近想加強一下vue的學習,畢竟是現在最火的前端框架嘛。那麼,我們這次通過一個購物車的效果,讓大家瞭解一下vue2.0中指令的基本用法。首先先看效果
我這裏的服務器不採用vue-cli的腳手架生成,直接通過xmapp的apache環境。
在頭部引入bootstrap.css,vue.js和axios,
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript" src="js/vue.js" ></script>
<link rel="stylesheet" href="css/bootstrap.css" />
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
然後添加HTML結構,
HTML
<div class="container" id="app">
<table class="table table-bordered text-center table-hover">
<tr>
<td>
編號
</td>
<td>
商品名稱
</td>
<td>
價格
</td>
<td>
數量
</td>
<td>
購買操作
</td>
<td>
總計
</td>
<td>
操作
</td>
</tr>
<tr v-for="(data,index) in tableList">
<td>
{{data.id}}
</td>
<td>
{{data.name}}
</td>
<td>
{{data.price}}
</td>
<td>
{{data.number}}
</td>
<td>
<button class="btn" @click="add(index)">+</button>
<button class="btn" @click="reduce(index)">-</button>
</td>
<td>
{{data.price*data.number}}
</td>
<td>
<button class="btn btn-primary" @click="del(index)">刪除</button>
</td>
</tr>
<tr>
<td colspan="7">
總計金額:<span>{{sum}}</span>
總計數量:<span>{{count}}</span>
</td>
</tr>
</table>
</div>
開始編寫js
new Vue({
el:"#app",
data:{
tableList:[]
},
methods:{
add:function(i){
this.tableList[i].number++
this.tableList[i].sum = parseInt(this.tableList[i].number)*parseInt(this.tableList[i].price)
console.log(parseInt(this.tableList[i].price))
},
reduce:function(i){
var num = this.tableList[i].number--
if(num<=0){
this.tableList[i].number = 0
}
this.tableList[i].sum = parseInt(this.tableList[i].number)*parseInt(this.tableList[i].price)
},
del:function(i){
//這個地方是個大坑
var _this = this
var a = this.tableList[i].id
//這個地方是個大坑
var params = new URLSearchParams();
params.append('id', a);
//這個地方是個大坑
axios.post('static/del.php',params)
.then(function(res){
_this.tableList.splice(i,1)
})
.catch(function(res){
alert(2)
})
}
},
computed:{
sum:function(){
var all = 0
for (var i=0;i<this.tableList.length;i++) {
all += parseInt(this.tableList[i].sum)
}
return all
},
count:function(){
var all = 0
for (var i=0;i<this.tableList.length;i++) {
all += parseInt(this.tableList[i].number)
}
return all
}
},
mounted:function(){
var _this = this
axios.get('static/table.php')
.then(function (response) {
if(response.status==200){
_this.tableList = response.data
console.log(response.data)
}
})
.catch(function (error) {
console.log(error);
});
}
})
大家要注意,如果你是用axios發起post請求,傳遞參數的時候要千萬小心了,你如果按照文檔的方式來做就坑爹了,要注意加上以下幾行代碼:
var params = new URLSearchParams();
params.append('id', a);
axios.post('static/del.php',params)
.then(function(res){
_this.tableList.splice(i,1)
})
.catch(function(res){
alert(2)
})
我們來看下數據庫結構:
看下php接口怎麼寫,
del.php
<?php
$conn = mysqli_connect("localhost", "root","","axios");
if(!$conn){
echo "連接失敗了";
}
$id = $_POST["id"];
mysqli_set_charset($conn,"utf8");
$sql = "delete from table_list where id='$id'";
$res = mysqli_query($conn, $sql);
if($res){
echo 1; //刪除成功
}else{
echo 2;//刪除失敗
}
?>
數據加載接口,table.php
<?php
$conn = mysqli_connect("localhost", "root","","axios");
if(!$conn){
echo "連接失敗了";
}
mysqli_set_charset($conn,"utf8");
$sql = "select * from table_list";
$res = mysqli_query($conn, $sql);
$data = array();
while($row = mysqli_fetch_assoc($res)){
$data[] = $row;
}
echo json_encode($data,JSON_UNESCAPED_UNICODE);
?>