Vue 간이 카 트 사례 실현
일단 완성 후의 효 과 를 살 펴 보 자.
CSS 부분
여 기 는 할 말 이 없다.바로 v-cloak 이라는 지식 점 이다.
table{
border: 1px solid #e9e9e9;
border-collapse: collapse;
border-spacing: 0;
}
th,td{
padding: 8px 16px;
border: 1px solid #e9e9e9;
text-align: center;
}
th{
background-color: #f7f7f7;
color: #5c6b77;
font-weight: 600;
}
[v-cloak]{
display: none;
}
HTML 부분여기에 사 용 된 Vue 의 지식 포 인 트 를 설명 합 니 다.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> </title>
<link rel="stylesheet" href="style.css" >
</head>
<body>
<div id="app" v-cloak>
<div v-if="books.length">
<table>
<thead>
<tr>
<th></th>
<th> </th>
<th> </th>
<th> </th>
<th> </th>
<th> </th>
</tr>
</thead>
<tbody>
<tr v-for="(item,index) in books">
<th>{{item.id}}</th>
<th>{{item.name}}</th>
<th>{{item.date}}</th>
<!-- -->
<!-- <th>{{"¥"+item.price.toFixed(2)}}</th> -->
<!-- -->
<!-- <th>{{getFinalPrice(item.price)}}</th> -->
<!-- -->
<th>{{item.price | showPrice}}</th>
<th>
<button @click="decrement(index)" :disabled="item.count<=0">-</button>
{{item.count}}
<button @click="increment(index)">+</button>
</th>
<th><button @click="removeHandle(index)"> </button></th>
</tr>
</tbody>
</table>
<h2> :{{totalPrice | showPrice}}</h2>
</div>
<h2 v-else>
</h2>
</div>
</body>
<script src="../js/vue.js"></script>
<script src="main.js"></script>
</html>
JS 부분
const app = new Vue({
el:"#app",
data:{
books:[
{
id:1,
name:"《 》",
date:'2006-9',
price:85.00,
count:1
},
{
id:2,
name:"《UNIX 》",
date:'2006-2',
price:50.00,
count:1
},
{
id:3,
name:"《 》",
date:'2008-10',
price:39.00,
count:1
},
{
id:4,
name:"《 》",
date:'2006-3',
price:128.00,
count:1
},
]
},
methods: {
// , , 。
// getFinalPrice(price){
// return "¥"+price.toFixed(2)
// },
increment(index){
this.books[index].count++
},
decrement(index){
this.books[index].count--
},
removeHandle(index){
this.books.splice(index,1);
}
},
computed: {
totalPrice(){
// : for
// let totalPrice = 0;
// for(let i=0;i<this.books.length;i++){
// totalPrice += this.books[i].price * this.books[i].count
// }
// return totalPrice
// :for in
// let totalPrice = 0;
// for(let i in this.books){
// // console.log(i);//1 2 3 4
// totalPrice += this.books[i].price * this.books[i].count
// }
// return totalPrice
// :for of
// let totalPrice = 0;
// for(let item of this.books){
// // console.log(item);//
// totalPrice += item.price * item.count
// }
// return totalPrice
// :reduce
return this.books.reduce(function (preValue, book) {
// console.log(book);//
return preValue + book.price * book.count
}, 0)
}
},
//
filters:{
showPrice(price){
return "¥"+price.toFixed(2)
}
}
})
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Fastapi websocket 및 vue 3(Composition API)1부: FastAPI virtualenv 만들기(선택 사항) FastAPI 및 필요한 모든 것을 다음과 같이 설치하십시오. 생성main.py 파일 및 실행 - 브라우저에서 이 링크 열기http://127.0.0.1:...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.