NenTang.vn
FrontendVueJS căn bản
Tạo trang Kiểm tra Giỏ hàng (cart-checkout)
Chương 4 · Bài 9
Dự án thực tế mẫu - Trang web Bán hàng VueJS
Thời lượng: 48 phútĐọc khoảng: 3 phút
Từ viết tắt
Nội dung bài học không khai báo từ viết tắt bằng thẻ abbr.
Nội dung bài học
Step 1: tạo trang Kiểm tra giỏ hàng CartCheckout
- Tạo file
project-nentang/src/pages/CartCheckout.vue - Nội dung file như sau:
javascript
1<template>2 <section>3 <div class="container">4 <div class="row">5 <div class="col">6 <h1 class="text-center">Giỏ hàng</h1>7 </div>8 </div>9 10 <div class="row">11 <div class="col">12 <table class="table table-borderd table-striped">13 <thead>14 <th>STT</th>15 <th>Hình đại diện</th>16 <th>Tên sản phẩm</th>17 <th>Giá</th>18 <th>Số lượng</th>19 <th>Thành tiền</th>20 <th>#</th>21 </thead>22 <tbody>23 <tr24 v-for="(product, index) in getProductsInCart"25 v-bind:key="product.id"26 >27 <td>{{ index + 1 }}</td>28 <td>29 <img30 :src="product.image"31 alt=""32 class="product-item-in-cart-img"33 />34 </td>35 <td>{{ product.name }}</td>36 <td class="text-right">{{ product.price }}</td>37 <td class="text-right">{{ product.totalBuyed }}</td>38 <td class="text-right">39 {{ product.price * product.totalBuyed }}40 </td>41 <td>42 <button class="btn btn-danger" @click="remove(product.id)">43 <font-awesome-icon :icon="['fas', 'trash']" /> Xóa44 </button>45 </td>46 </tr>47 </tbody>48 <tfoot>49 <tr>50 <td colspan="5" class="text-right">Tổng tiền:</td>51 <td class="text-right font-weight-bold">52 {{ getTotalAmount }}53 </td>54 </tr>55 </tfoot>56 </table>57 </div>58 </div>59 </div>60 </section>61</template>62 63<script>64// Import helper mapGetters của Vuex để có thể sử dụng getters `getAllProducts` đã khai báo bên KHO chứa (store)65// Import helper mapActions của Vuex để có thể sử dụng actions `removeProduct` đã khai báo bên KHO chứa (store)66import { mapGetters, mapActions } from 'vuex'67 68export default {69 computed: {70 ...mapGetters(['getProductsInCart']),71 72 // Hàm dùng để tính Tổng tiền73 getTotalAmount () {74 // Duyệt qua tất cả sản phẩm có trong giỏ hàng75 // Thành tiền += Giá * Số lượng đã mua76 let totalAmount = 077 this.getProductsInCart.forEach(product => {78 totalAmount += product.price * product.totalBuyed79 })80 return totalAmount81 }82 },83 methods: {84 ...mapActions(['removeProduct']),85 86 // Hàm xóa Sản phẩm khỏi Giỏ hàng87 remove (id) {88 this.removeProduct(id)89 }90 }91}92</script>93 94<style scoped>95.product-item-in-cart-img {96 width: 150px;97 height: 150px;98 object-fit: contain;99}100</style>Step 2: tạo route Trang Kiểm tra Giỏ hàng /kiem-tra-gio-hang
- Hiệu chỉnh file
project-netashop/src/router/index.js - Nội dung file như sau:
javascript
1import Vue from 'vue'2import Router from 'vue-router'3 4Vue.use(Router)5 6/* Chúng ta sử dụng thuộc tính `meta` trong routes để khai báo:7- Ứng với path / -> sẽ sử dụng layout `default`8- Ứng với path /gioi-thieu -> sẽ sử dụng layout `no-sidebar`9- Ứng với path /lien-he -> sẽ sử dụng layout `no-sidebar`10- Ứng với path /kiem-tra-gio-hang -> sẽ sử dụng layout `no-sidebar`11- Ứng với path không tìm thấy 404 -> sẽ sử dụng layout `no-sidebar`12*/13const routes = [14 {15 path: '/',16 name: 'home',17 meta: {18 layout: 'default'19 },20 // component: require('@/pages/Home.vue').default21 component: () => import('@/pages/Home.vue')22 },23 {24 path: '/gioi-thieu',25 name: 'about',26 meta: {27 layout: 'no-sidebar'28 },29 component: () => import('@/pages/About.vue')30 },31 {32 path: '/lien-he',33 name: 'contact',34 meta: {35 layout: 'no-sidebar'36 },37 component: () => import('@/pages/Contact.vue')38 },39 {40 path: '/kiem-tra-gio-hang',41 name: 'cartCheckout',42 meta: {43 layout: 'no-sidebar'44 },45 component: () => import('@/pages/CartCheckout.vue')46 },47 {48 path: '*',49 name: '404',50 meta: {51 layout: 'no-sidebar'52 },53 component: () => import('@/pages/404.vue')54 }55]56 57// Để đường dẫn Path đẹp hơn và thân thiện với người dùng và SEO58// Chúng ta sử dụng History mode thay vì HashTag mode (dấu #)59const router = new Router({60 mode: 'history',61 routes: routes62})63 64export default router
