
{"id":7565,"date":"2023-07-24T04:25:21","date_gmt":"2023-07-24T04:25:21","guid":{"rendered":"https:\/\/tapchicntt.com\/?p=7565"},"modified":"2023-09-06T11:12:06","modified_gmt":"2023-09-06T04:12:06","slug":"tao-ung-dung-gio-hang-su-dung-vue-js-vuex","status":"publish","type":"post","link":"https:\/\/tapchicntt.com\/tao-ung-dung-gio-hang-su-dung-vue-js-vuex\/","title":{"rendered":"T\u1ea1o \u1ee9ng d\u1ee5ng gi\u1ecf h\u00e0ng s\u1eed d\u1ee5ng Vue.js &#8211; Vuex"},"content":{"rendered":"\n<p>\u0110\u1ec3 t\u1ea1o m\u1ed9t \u1ee9ng d\u1ee5ng gi\u1ecf h\u00e0ng s\u1eed d\u1ee5ng Vue.js, b\u1ea1n c\u1ea7n t\u1ea1o m\u1ed9t \u1ee9ng d\u1ee5ng Vue m\u1edbi v\u00e0 tri\u1ec3n khai c\u00e1c t\u00ednh n\u0103ng c\u01a1 b\u1ea3n c\u1ee7a gi\u1ecf h\u00e0ng. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 h\u01b0\u1edbng d\u1eabn c\u01a1 b\u1ea3n \u0111\u1ec3 b\u1ea1n b\u1eaft \u0111\u1ea7u:<\/p>\n\n\n\n<p><strong>B\u01b0\u1edbc 1: T\u1ea1o d\u1ef1 \u00e1n Vue<\/strong><\/p>\n\n\n\n<p>\u0110\u1ea3m b\u1ea3o b\u1ea1n \u0111\u00e3 c\u00e0i \u0111\u1eb7t Node.js v\u00e0 Vue CLI tr\u00ean m\u00e1y t\u00ednh c\u1ee7a b\u1ea1n.<\/p>\n\n\n\n<p>M\u1edf Command Prompt ho\u1eb7c Terminal v\u00e0 t\u1ea1o m\u1ed9t d\u1ef1 \u00e1n Vue m\u1edbi b\u1eb1ng c\u00e1ch ch\u1ea1y l\u1ec7nh sau:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nvue create gio-hang\n<\/pre><\/div>\n\n\n<p>Ch\u1ecdn c\u00e1c c\u00e0i \u0111\u1eb7t m\u1eb7c \u0111\u1ecbnh ho\u1eb7c t\u00f9y ch\u1ec9nh theo nhu c\u1ea7u c\u1ee7a b\u1ea1n khi t\u1ea1o d\u1ef1 \u00e1n Vue.<\/p>\n\n\n\n<p><strong>B\u01b0\u1edbc 2: T\u1ea1o c\u00e1c th\u00e0nh ph\u1ea7n Vue<\/strong><\/p>\n\n\n\n<p>Trong d\u1ef1 \u00e1n Vue c\u1ee7a b\u1ea1n, t\u1ea1o c\u00e1c th\u00e0nh ph\u1ea7n Vue c\u01a1 b\u1ea3n \u0111\u1ec3 \u0111\u1ea1i di\u1ec7n cho gi\u1ecf h\u00e0ng, s\u1ea3n ph\u1ea9m v\u00e0 danh s\u00e1ch s\u1ea3n ph\u1ea9m.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n&lt;!-- GioHang.vue --&gt;\n&lt;template&gt;\n  &lt;div&gt;\n    &lt;h2&gt;Gi\u1ecf h\u00e0ng&lt;\/h2&gt;\n    &lt;ul&gt;\n      &lt;li v-for=&quot;item in cartItems&quot; :key=&quot;item.id&quot;&gt;\n        {{ item.name }} - {{ item.price }} \u0111\u1ed3ng\n      &lt;\/li&gt;\n    &lt;\/ul&gt;\n    &lt;p&gt;T\u1ed5ng c\u1ed9ng: {{ total }} \u0111\u1ed3ng&lt;\/p&gt;\n  &lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\nexport default {\n  computed: {\n    cartItems() {\n      \/\/ Tr\u1ea3 v\u1ec1 danh s\u00e1ch c\u00e1c s\u1ea3n ph\u1ea9m trong gi\u1ecf h\u00e0ng\n      return this.$store.state.cartItems;\n    },\n    total() {\n      \/\/ T\u00ednh t\u1ed5ng gi\u00e1 tr\u1ecb c\u00e1c s\u1ea3n ph\u1ea9m trong gi\u1ecf h\u00e0ng\n      return this.cartItems.reduce((total, item) =&gt; total + item.price, 0);\n    },\n  },\n};\n&lt;\/script&gt;\n<\/pre><\/div>\n\n\n<p><strong>B\u01b0\u1edbc 3: Qu\u1ea3n l\u00fd d\u1eef li\u1ec7u v\u1edbi Vuex<\/strong><\/p>\n\n\n\n<p>T\u1ea1o m\u1ed9t Vuex store \u0111\u1ec3 qu\u1ea3n l\u00fd tr\u1ea1ng th\u00e1i gi\u1ecf h\u00e0ng v\u00e0 c\u00e1c h\u00e0nh \u0111\u1ed9ng li\u00ean quan \u0111\u1ebfn gi\u1ecf h\u00e0ng.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/\/ Trong store\/index.js\nimport Vue from &#039;vue&#039;;\nimport Vuex from &#039;vuex&#039;;\n\nVue.use(Vuex);\n\nexport default new Vuex.Store({\n  state: {\n    cartItems: &#x5B;],\n  },\n  mutations: {\n    addToCart(state, product) {\n      state.cartItems.push(product);\n    },\n    clearCart(state) {\n      state.cartItems = &#x5B;];\n    },\n  },\n  actions: {\n    addToCart({ commit }, product) {\n      commit(&#039;addToCart&#039;, product);\n    },\n    clearCart({ commit }) {\n      commit(&#039;clearCart&#039;);\n    },\n  },\n});\n<\/pre><\/div>\n\n\n<p><strong>B\u01b0\u1edbc 4: T\u00edch h\u1ee3p gi\u1ecf h\u00e0ng v\u00e0o \u1ee9ng d\u1ee5ng Vue<\/strong><\/p>\n\n\n\n<p>Trong file main.js, nh\u1eadp Vuex store v\u00e0 s\u1eed d\u1ee5ng n\u00f3 \u0111\u1ec3 chia s\u1ebb d\u1eef li\u1ec7u gi\u1ecf h\u00e0ng v\u1edbi to\u00e0n b\u1ed9 \u1ee9ng d\u1ee5ng.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/\/ Trong main.js\nimport Vue from &#039;vue&#039;;\nimport App from &#039;.\/App.vue&#039;;\nimport store from &#039;.\/store&#039;;\n\nVue.config.productionTip = false;\n\nnew Vue({\n  store,\n  render: (h) =&gt; h(App),\n}).$mount(&#039;#app&#039;);\n<\/pre><\/div>\n\n\n<p><strong>B\u01b0\u1edbc 5: S\u1eed d\u1ee5ng th\u00e0nh ph\u1ea7n Gi\u1ecf h\u00e0ng trong \u1ee9ng d\u1ee5ng Vue<\/strong><\/p>\n\n\n\n<p>Trong file App.vue, s\u1eed d\u1ee5ng th\u00e0nh ph\u1ea7n Gi\u1ecf h\u00e0ng \u0111\u1ec3 hi\u1ec3n th\u1ecb gi\u1ecf h\u00e0ng.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n&lt;template&gt;\n  &lt;div id=&quot;app&quot;&gt;\n    &lt;h1&gt;\u1ee8ng d\u1ee5ng Gi\u1ecf h\u00e0ng Vue&lt;\/h1&gt;\n    &lt;GioHang \/&gt;\n  &lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\nimport GioHang from &#039;.\/components\/GioHang&#039;;\n\nexport default {\n  components: {\n    GioHang,\n  },\n};\n&lt;\/script&gt;\n<\/pre><\/div>\n\n\n<p><strong>B\u01b0\u1edbc 6: T\u00edch h\u1ee3p s\u1ea3n ph\u1ea9m v\u00e0 ch\u1ee9c n\u0103ng th\u00eam v\u00e0o gi\u1ecf h\u00e0ng<\/strong><\/p>\n\n\n\n<p>Trong trang s\u1ea3n ph\u1ea9m ho\u1eb7c danh s\u00e1ch s\u1ea3n ph\u1ea9m c\u1ee7a b\u1ea1n, s\u1eed d\u1ee5ng c\u00e1c n\u00fat ho\u1eb7c s\u1ef1 ki\u1ec7n \u0111\u1ec3 th\u00eam s\u1ea3n ph\u1ea9m v\u00e0o gi\u1ecf h\u00e0ng th\u00f4ng qua Vuex store.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n&lt;template&gt;\n  &lt;div&gt;\n    &lt;h2&gt;Danh s\u00e1ch s\u1ea3n ph\u1ea9m&lt;\/h2&gt;\n    &lt;ul&gt;\n      &lt;li v-for=&quot;product in products&quot; :key=&quot;product.id&quot;&gt;\n        {{ product.name }} - {{ product.price }} \u0111\u1ed3ng\n        &lt;button @click=&quot;addToCart(product)&quot;&gt;Th\u00eam v\u00e0o gi\u1ecf h\u00e0ng&lt;\/button&gt;\n      &lt;\/li&gt;\n    &lt;\/ul&gt;\n  &lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\nexport default {\n  data() {\n    return {\n      products: &#x5B;\n        { id: 1, name: &#039;S\u1ea3n ph\u1ea9m 1&#039;, price: 100000 },\n        { id: 2, name: &#039;S\u1ea3n ph\u1ea9m 2&#039;, price: 200000 },\n        \/\/ Th\u00eam s\u1ea3n ph\u1ea9m kh\u00e1c t\u00f9y \u00fd\n      ],\n    };\n  },\n  methods: {\n    addToCart(product) {\n      this.$store.dispatch(&#039;addToCart&#039;, product);\n    },\n  },\n};\n&lt;\/script&gt;\n<\/pre><\/div>\n\n\n<p>\u0110\u00e2y ch\u1ec9 l\u00e0 m\u1ed9t c\u00e1ch c\u01a1 b\u1ea3n \u0111\u1ec3 t\u1ea1o m\u1ed9t \u1ee9ng d\u1ee5ng gi\u1ecf h\u00e0ng s\u1eed d\u1ee5ng Vue.js. B\u1ea1n c\u00f3 th\u1ec3 m\u1edf r\u1ed9ng v\u00e0 t\u00f9y ch\u1ec9nh ch\u1ee9c n\u0103ng theo \u00fd mu\u1ed1n, bao g\u1ed3m th\u00eam t\u00ednh n\u0103ng x\u00f3a s\u1ea3n ph\u1ea9m kh\u1ecfi gi\u1ecf h\u00e0ng, t\u0103ng\/gi\u1ea3m s\u1ed1 l\u01b0\u1ee3ng s\u1ea3n ph\u1ea9m, t\u00ednh t\u1ed5ng gi\u00e1 tr\u1ecb gi\u1ecf h\u00e0ng&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0110\u1ec3 t\u1ea1o m\u1ed9t \u1ee9ng d\u1ee5ng gi\u1ecf h\u00e0ng s\u1eed d\u1ee5ng Vue.js, b\u1ea1n c\u1ea7n t\u1ea1o m\u1ed9t \u1ee9ng d\u1ee5ng Vue m\u1edbi v\u00e0 tri\u1ec3n [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":7574,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[242],"tags":[],"class_list":["post-7565","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vue-js"],"views":601,"_links":{"self":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/7565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/comments?post=7565"}],"version-history":[{"count":13,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/7565\/revisions"}],"predecessor-version":[{"id":8581,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/7565\/revisions\/8581"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/media\/7574"}],"wp:attachment":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/media?parent=7565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/categories?post=7565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/tags?post=7565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}