Học VueJS là phải biết VUEX
VUEX nói đơn giản chính là Kho chứa dữ liệu tập trung, dùng chung cho tất cả các component trong ứng dụng VueJS. Xem hình sau để hình dung về kho chứa dữ liệu tập trungStep 1: sử dụng NPM (Node packages manager) để cài đặt thư viện Vuex
- Trỏ đường dẫn
cmdvào thư mục gốcproject-netashop. - Thực thi câu lệnh sau:
shellscript
1npm install vuexStep 2: khởi tạo kho dữ liệu dùng chung cho các components
- Tạo thư mục
project-nentang/src/store/ - Tạo file
project-nentang/src/store/store.js - Nội dung file như sau:
javascript
1import Vue from 'vue'2import Vuex from 'vuex'3 4Vue.use(Vuex)5 6export default new Vuex.Store({7 // State là Trạng thái => hay còn gọi là Data (dữ liệu) của KHO chứa (store)8 // Tất cả dữ liệu sẽ được khởi tạo trong thuộc tính `state`9 state: {10 products: [11 {12 id: 1,13 name: 'Notebook Lenovo Ideapad 320 Intel® Core i5-7200u 8GB',14 price: 2259,15 image: require('@/assets/img/product/ipad4.png'),16 stars: 5,17 totalReviews: 230,18 details: 'Mô tả chi tiết về sản phẩm Notebook Lenovo',19 totalBuyed: 020 },21 {22 id: 2,23 name: 'Notebook Dell Inspiron i15-3567-A30P Intel Core 7ª i5 4GB',24 price: 2284,25 image: require('@/assets/img/product/samsung-galaxy-tab.jpg'),26 stars: 3.4,27 totalReviews: 20,28 details: 'Mô tả chi tiết về sản phẩm Notebook Dell Inspiron',29 totalBuyed: 030 },31 {32 id: 3,33 name: 'Notebook Samsung Essentials E21 Intel Celeron Dual Core',34 price: 1490,35 image: require('@/assets/img/product/iphone5.jpg'),36 stars: 1,37 totalReviews: 1,38 details: 'Mô tả chi tiết về sản phẩm Notebook Samsung Essentials',39 totalBuyed: 040 },41 {42 id: 4,43 name: 'Notebook Samsung Expert X22 Intel Core 7 i5 8GB',44 price: 2307,45 image: require('@/assets/img/product/nokia-asha-311.jpg'),46 stars: 4.4,47 totalReviews: 340,48 details: 'Mô tả chi tiết về sản phẩm Notebook Samsung Expert',49 totalBuyed: 050 }51 ],52 53 cartProducts: [],54 currentProduct: {}55 },56 57 // Getters: là các thuộc tính dùng để các component lấy dữ liệu từ KHO chứa (store) về58 getters: {59 getAllProducts: state => state.products,60 getProductsInCart: state => state.cartProducts,61 getCurrentProduct: state => state.currentProduct62 },63 64 // Mutations: là các thuộc tính dùng để thay đổi giá trị bên trong KHO chứa (store)65 // thay đổi giá trị, nhưng chạy ĐỒNG BỘ (SYNC)66 mutations: {67 ADD_PRODUCT: (state, product) => {68 let o = state.cartProducts.find(o => o.id === product.id) // Tìm sản phẩm trong Giỏ hàng theo ID69 if (!o) { // Nếu sản phẩm chưa có trong Giỏ hàng => Thêm sản phẩm vào giỏ hàng70 product.totalBuyed = 171 state.cartProducts.push(Object.assign({}, product))72 } else { // Nếu sản phẩm đã có => cộng đồn số lượng mua lên 1 đơn vị73 o.totalBuyed += 174 }75 },76 REMOVE_PRODUCT: (state, id) => {77 let o = state.cartProducts.find(o => o.id === id) // Tìm sản phẩm trong Giỏ hàng theo ID78 let index = state.cartProducts.indexOf(o)79 state.cartProducts.splice(index, 1)80 },81 CURRENT_PRODUCT: (state, product) => {82 state.currentProduct = product83 }84 },85 86 // Actions: là các hàm (methods) dùng để thực hiện các hành động thay đổi giá trị bên trong KHO chứa (store)87 // tương tự như Mutations, nhưng có thể chạy BẤT ĐỒNG BỘ (ASYNC)88 actions: {89 addProduct: (context, product) => {90 context.commit('ADD_PRODUCT', product)91 },92 removeProduct: (context, id) => {93 context.commit('REMOVE_PRODUCT', id)94 },95 currentProduct: (context, product) => {96 context.commit('CURRENT_PRODUCT', product)97 }98 }99})Step 3: khai báo cho VueJS biết chúng ta sử dụng KHO dữ liệu dùng chung store
- Hiệu chỉnh file
project-nentang/src/main.js - Nội dung file như sau:
javascript
1// The Vue build version to load with the `import` command2// (runtime-only or standalone) has been set in webpack.base.conf with an alias.3import Vue from 'vue'4import App from './App'5import router from './router'6import store from './store/store.js'7 8Vue.config.productionTip = false9 10// Import thư viện Bootstrap (JS và CSS)11import 'bootstrap'12import 'bootstrap/dist/css/bootstrap.min.css'13 14// Import thư viện Font Awesome15import { library } from '@fortawesome/fontawesome-svg-core'16import { fab } from '@fortawesome/free-brands-svg-icons'17import { fas } from '@fortawesome/free-solid-svg-icons'18import { far } from '@fortawesome/free-regular-svg-icons'19import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'20 21// Đăng ký component Global Layouts22import Default from './layouts/LayoutDefault.vue'23import NoSidebar from './layouts/LayoutNoSidebar.vue'24 25library.add(fab, fas, far)26Vue.component('font-awesome-icon', FontAwesomeIcon)27Vue.component('default-layout', Default)28Vue.component('no-sidebar-layout', NoSidebar)29 30 31/* eslint-disable no-new */32new Vue({33 el: '#app',34 router,35 store,36 components: { App },37 template: '<App/>'38})
