43.Vueの第1段階の小さな演習(自分でショッピングカートのページを作成する)

コードはコードクラウドに配置されます。自分でプルできます:https//gitee.com/cxy-xupeng/vue-test.git

バックエンドプログラマーとして、最初の2つの章の内容をどのように注意深く読んでから、ここで小さな演習を見てみましょう。

1つ、小さな演習1

配列リストの場合、クリックした人は誰でも赤になります。

分析:まず、リストをトラバースする必要があります。次に、誰かを赤くしたい場合は、各リストデータを取得できる必要があります。つまり、リストのシリアル番号が必要です。第三に、クリックイベントも必要です。第四に、あなたはスタイルが必要です

 

2、小さな運動2

1.書籍のショッピングカートの場合は、[+]、書籍の数量+1、合計金額に書籍の金額を加えたものをクリックします。"-" 同じです。

2. [削除]をクリックしてブックを削除します。

3.ショッピングカート全体に本がない場合は、テキスト行を表示します

最初に棚を作りましょう:

いくつかの詳細を扱い始めましょう:

(1)価格は小数点第2位を維持する必要があり、その前に¥があります。'¥' + price.toFixed(2)

ここで価格を設定する方法も2つあります

1)以前に学んだ方法

js

html里:

2)フィルター

jsの場合:

html里:

(2)価格の「+」と「-」

1)対応する番号をクリックして、変更を確認します

2)1になった後は減らさないでください

(3)[削除]をクリックして、データの行を削除します。そして、テキストの行を表示します

(4)最終価格を表示する

この時点で、コードは完成しています。

完全なコード:

index.html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<!-- 引入style.css -->
		<link rel="stylesheet" href="style.css" />
	</head>
	<body>
		<div id="app">
			<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="(book,index) in books">
							<td>{
   
   {book.id}}</td>
							<td>{
   
   {book.name}}</td>
							<td>{
   
   {book.date}}</td>
							<td>
								<button @click="decrement(index)" :disabled="book.count<=1">-</button>
								{
   
   {book.price | showPrice}}
								<button @click="increment(index)">+</button>
							</td>
							<td>{
   
   {book.count}}</td>
							<td>
								<button @click="removeHandle(index)">移除</button>
							</td>
						</tr>
					</tbody>
				</table>
				<h2>总价格:{
   
   {totalPrice | showPrice}}</h2>
			</div>
			<div v-else>购物车为空</div>
		</div>
		
		
		<!-- 引入js -->
		<script src="../js/vue.js"></script>
		<script src="main.js"></script>
	</body>
</html>

main.js

const app = new Vue({
	el:"#app",
	data:{
		books:[
			{
				id:1,
				name:'java',
				date:'2020-11',
				price:85.00,
				count:1
			},
			{
				id:2,
				name:'linux',
				date:'2020-11',
				price:59.00,
				count:1
			},
			{
				id:3,
				name:'python',
				date:'2020-11',
				price:39.00,
				count:1
			},
			{
				id:4,
				name:'java',
				date:'2020-11',
				price:128.00,
				count:1
			}
		]
	},
	methods:{
		/* getFinalPrice(price){
			return '¥'+price.toFixed(2)
		} */
		decrement(index){
			this.books[index].count--
		},
		increment(index){
			this.books[index].count++
		},
		removeHandle(index){
			this.books.splice(index,1)
		}
	},
	filters:{
		showPrice(price){
			return '¥'+price.toFixed(2)
		}
	},
	computed:{
		totalPrice(){
			let totalPrice =0
			for(let i = 0;i<this.books.length;i++){
				totalPrice += this.books[i].count * this.books[i].price
			}
			return totalPrice
		}
	}
})

style.css

table{
	border:1px solid #e9e9e9;
	/* border-collapse 属性设置表格的边框是否被合并为一个单一的边框,还是象在标准的 HTML 中那样分开显示。 */
	border-collapse: collapse;
	/* border-spacing 属性设置相邻单元格的边框间的距离 */
	border-spacing: 0;
}

th,td{
	padding: 8px 16px;
	border: 1px solid #e9e9e9;
	text-align: left;
}

th{
	background-color: #f7f7f7;
	color: #5c6b77;
	font-weight: 600;
}

 

おすすめ

転載: blog.csdn.net/qq_40594696/article/details/110119688