博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于vuex购物车实现的原理
阅读量:5966 次
发布时间:2019-06-19

本文共 9765 字,大约阅读时间需要 32 分钟。

1、模拟购物车功能

创建store/cart.js

export default {

state:{

cartlist: []

},

getters: {

},

actions: {

},

mutations: {

}

}

2、添加该模块到状态管理器

import Vue from 'vue'

import Vuex from 'vuex'

import cart from './cart.js'

Vue.use(Vuex)

const store = new Vuex.Store({

modules: {

cart

}

})

export default store

3、cart组件中script.js

import {mapState} from 'vuex'

export default {

computed: {

...mapState({

'cartlist': ({cart}) => cart.cartlist

})

}

}

4、在store/cart.js中请求购物车数据

export default {

state:{

cartlist: []

},

getters: {

},

actions: {

getCartlist ({commit}) {

cartapi.getCartlist((data) => {commit('changeCartlist', data)})

}

},

mutations: {

}

}

5、实现请求数据的方法api/cart.js

import axios from 'axios'

export default {

getCartlist (cb) {

// axios请求数据,请求成功调用回调函数cb ---- 模拟数据data

var data = [

{id:1,name:'pro1',num:1,price:10},

{id:2,name:'pro2',num:2,price:20},

{id:3,name:'pro3',num:3,price:30},

{id:4,name:'pro4',num:4,price:40}

]

cb(data)

}

}

6、实现提交mutation改变cartlist----store/cart.js

export default {

state:{

cartlist: []

},

getters: {

},

actions: {

getCartlist ({commit}) {

cartapi.getCartlist((data) => {commit('changeCartlist', data)})

}

},

mutations: {

changeCartlist (state, data) {

state.cartlist = data

},

}

}

7、触发请求数据----cart组件中分发action----script.js

import {mapState} from 'vuex'

export default {

computed: {

...mapState({

'cartlist': ({cart}) => cart.cartlist

})

},

mounted () {

this.$store.dispatch('getCartlist')

}

}

8、计算商品的总数量和总价

8.1 store/cart.js中添加选项getters

export default {

state:{

cartlist: []

},

getters: {

totalCount(state){

let totalNum = 0

let totalPrice = 0

state.cartlist.filter((item) => {

totalNum += item.num * 1

totalPrice += item.num * item.price

})

return {

totalNum,

totalPrice

}

}

},

actions: {

getCartlist ({commit}) {

cartapi.getCartlist((data) => {commit('changeCartlist', data)})

}

},

mutations: {

changeCartlist (state, data) {

state.cartlist = data

},

}

}

8.2 cart组件中index.vue使用即可

8.2.1 直接获取store中的getters

<div>总量:{

{this.$store.getters.totalCount.totalNum}}</div>

<div>总价:{

{this.$store.getters.totalCount.totalPrice}}</div

8.2.2 使用mapGetters辅助函数减少代码量

script.js

import {mapState,mapGetters} from 'vuex'

export default {

computed: {

...mapState({

'cartlist': ({cart}) => cart.cartlist

}),

mapGetters(['totalCount'])

},

mounted () {

this.$store.dispatch('getCartlist')

}

}

index.vue

<div>总量:{

{totalCount.totalNum}}</div>

<div>总价:{

{totalCount.totalPrice}}</div

9、选择商品计算价格和数量

9.1 index.vue布局

<template>

<div>

<ul>

<li v-for='(item, index) in cartlist' :key="item.id">

<input type='checkbox' v-model='item.flag' />

{

{ item.name}} ---- {
{item.num}} ---- {
{item.price}} -

</li>

</ul>

<div>总量:{

{totalCount.totalNum}}</div>

<div>总价:{

{totalCount.totalPrice}}</div>

</div>

</template>

<script src='./script.js'></script>

<style src="./style.scss"></style>

9.2 改造源数据 api/cart.js

import axios from 'axios'

export default {

getCartlist (cb) {

// axios请求数据,请求成功调用回调函数cb ---- 模拟数据data

var data = [

{id:1,name:'pro1',num:1,price:10},

{id:2,name:'pro2',num:2,price:20},

{id:3,name:'pro3',num:3,price:30},

{id:4,name:'pro4',num:4,price:40}

]

data.map((item) => {

item.flag = false

})

cb(data)

}

}

10、 实现全选和反选功能

10.1 index.vue布局

<template>

<div>

全选<input type='checkbox' @change="selectFn(cartlist)" v-model='allSelected' />

<ul>

<li v-for='(item, index) in cartlist' :key="item.id">

<input type='checkbox' v-model='item.flag' />

{

{ item.name}} ---- {
{item.num}} ---- {
{item.price}} -

</li>

</ul>

<div>总量:{

{totalCount.totalNum}}</div>

<div>总价:{

{totalCount.totalPrice}}</div>

</div>

</template>

<script src='./script.js'></script>

<style src="./style.scss"></style>

10.2 store/cart.js添加新的初始值 allSelected

export default {

state:{

cartlist: [],

allSelected: false

},

getters: {

totalCount(state){

let totalNum = 0

let totalPrice = 0

state.cartlist.filter((item) => {

totalNum += item.num * 1

totalPrice += item.num * item.price

})

return {

totalNum,

totalPrice

}

}

},

actions: {

getCartlist ({commit}) {

cartapi.getCartlist((data) => {commit('changeCartlist', data)})

}

},

mutations: {

changeCartlist (state, data) {

state.cartlist = data

},

}

}

10.3 cart组件中添加全选事件

//index.vue

全选<input type='checkbox' @change="selectFn(cartlist)" v-model='allSelected' />

// script.js 中的methods

selectFn (cartlist) {

this.$store.commit('changeAllSelect') // 切换真假

//改变store中数据后立马请求其值

if(this.$store.state.cart.allSelected){ // 如果为真,表示全部要被选中

const flag = true // 标志为真

this.$store.dispatch('getAllSelectCartlist', {cartlist, flag} ) // action中设置所有元素为真

}else{

const flag = false

this.$store.dispatch('getAllSelectCartlist', {cartlist, flag} )

}

},

10.4 实现changeAllSelect mutation 和 getAllSelectCartlist action

store/cart.js

export default {

state:{

cartlist: [],

allSelected: false

},

getters: {

totalCount(state){

let totalNum = 0

let totalPrice = 0

state.cartlist.filter((item) => {

totalNum += item.num * 1

totalPrice += item.num * item.price

})

return {

totalNum,

totalPrice

}

}

},

actions: {

getCartlist ({commit}) {

cartapi.getCartlist((data) => {commit('changeCartlist', data)})

},

getAllSelectCartlist ({commit}, {cartlist, flag}) {

cartlist.map((item) => {

item.flag = flag

})

commit('changeCartlist', cartlist)

},

},

mutations: {

changeCartlist (state, data) {

state.cartlist = data

},

changeAllSelect (state) {

state.allSelected = !state.allSelected

},

}

}

11、点击列表选择实现全选和反选

<ul>

<li v-for='(item, index) in cartlist' :key="item.id">

<input type='checkbox' @change='selectItem(cartlist, item, index)' v-model='item.flag' />

{

{ item.name}} ---- {
{item.num}} ---- {
{item.price}} -- <button @click="deleteItem(cartlist, index)">删除</button>

</li>

</ul>

script.js

selectItem (cartlist, item, index) {

if(item.flag){

const flag = true

this.$store.dispatch('selectItem', {cartlist, index, flag}) // cartlist[index].flag = flag

}else {

const flag = false

this.$store.dispatch('selectItem', {cartlist, index, flag})

}

// console.log("cartlist",this.$store.state.cart.cartlist)

var cartlist = this.$store.state.cart.cartlist

this.$store.dispatch('checkItem',cartlist) // 每做出一次选择就去检测要不要全选

}

实现action selectItem 和checkItem store/cart.js

export default {

state:{

cartlist: [],

allSelected: false

},

getters: {

totalCount(state){

let totalNum = 0

let totalPrice = 0

state.cartlist.filter((item) => {

totalNum += item.num * 1

totalPrice += item.num * item.price

})

return {

totalNum,

totalPrice

}

}

},

actions: {

getCartlist ({commit}) {

cartapi.getCartlist((data) => {commit('changeCartlist', data)})

},

getAllSelectCartlist ({commit}, {cartlist, flag}) {

cartlist.map((item) => {

item.flag = flag

})

commit('changeCartlist', cartlist)

},

selectItem ({commit}, {cartlist, index, flag}) {

cartlist[index].flag = flag

commit('changeCartlist', cartlist)

},

checkItem ({commit}, cartlist){

let all = true

cartlist.map((item) => {

if(!item.flag) {

all = false

}

})

commit('checkCartlist', all)

}

},

mutations: {

changeCartlist (state, data) {

state.cartlist = data

},

changeAllSelect (state) {

state.allSelected = !state.allSelected

},

}

}

实现mutation checkCartlist

export default {

state:{

cartlist: [],

allSelected: false

},

getters: {

totalCount(state){

let totalNum = 0

let totalPrice = 0

state.cartlist.filter((item) => {

totalNum += item.num * 1

totalPrice += item.num * item.price

})

return {

totalNum,

totalPrice

}

}

},

actions: {

getCartlist ({commit}) {

cartapi.getCartlist((data) => {commit('changeCartlist', data)})

},

getAllSelectCartlist ({commit}, {cartlist, flag}) {

cartlist.map((item) => {

item.flag = flag

})

commit('changeCartlist', cartlist)

},

selectItem ({commit}, {cartlist, index, flag}) {

cartlist[index].flag = flag

commit('changeCartlist', cartlist)

},

checkItem ({commit}, cartlist){

let all = true

cartlist.map((item) => {

if(!item.flag) {

all = false

}

})

commit('checkCartlist', all)

}

},

mutations: {

changeCartlist (state, data) {

state.cartlist = data

},

changeAllSelect (state) {

state.allSelected = !state.allSelected

},

checkCartlist (state, data) {

state.allSelected = data

}

}

}

12、删除操作

<ul>

<li v-for='(item, index) in cartlist' :key="item.id">

<input type='checkbox' @change='selectItem(cartlist, item, index)' v-model='item.flag' />

{

{ item.name}} ---- {
{item.num}} ---- {
{item.price}} -- <button @click="deleteItem(cartlist, index)">删除</button>

</li>

</ul>

deleteItem (cartlist, index) {

this.$store.dispatch('deleteItem', {cartlist, index})

},

store/cart.js

export default {

state:{

cartlist: [],

allSelected: false

},

getters: {

totalCount(state){

let totalNum = 0

let totalPrice = 0

state.cartlist.filter((item) => {

totalNum += item.num * 1

totalPrice += item.num * item.price

})

return {

totalNum,

totalPrice

}

}

},

actions: {

getCartlist ({commit}) {

cartapi.getCartlist((data) => {commit('changeCartlist', data)})

},

getAllSelectCartlist ({commit}, {cartlist, flag}) {

cartlist.map((item) => {

item.flag = flag

})

commit('changeCartlist', cartlist)

},

selectItem ({commit}, {cartlist, index, flag}) {

cartlist[index].flag = flag

commit('changeCartlist', cartlist)

},

checkItem ({commit}, cartlist){

let all = true

cartlist.map((item) => {

if(!item.flag) {

all = false

}

})

commit('checkCartlist', all)

},

deleteItem ({commit}, {cartlist, index}) {

cartapi.deleteItem({cartlist, index}, (data) => {commit('changeCartlist', data)})

},

},

mutations: {

changeCartlist (state, data) {

state.cartlist = data

},

changeAllSelect (state) {

state.allSelected = !state.allSelected

},

checkCartlist (state, data) {

state.allSelected = data

}

}

}

api/cart.js

import axios from 'axios'

export default {

getCartlist (cb) {

// axios请求数据,请求成功调用回调函数cb ---- 模拟数据data

var data = [

{id:1,name:'pro1',num:1,price:10},

{id:2,name:'pro2',num:2,price:20},

{id:3,name:'pro3',num:3,price:30},

{id:4,name:'pro4',num:4,price:40}

]

data.map((item) => {

item.flag = false

})

cb(data)

},

deleteItem ({cartlist, index}, cb) {

cartlist.splice(index, 1)

cb(cartlist)

}

}

转载地址:http://ovtax.baihongyu.com/

你可能感兴趣的文章
Edison UVALive3488
查看>>
微信如何唤起外部浏览器打开指定链接
查看>>
linux之虚拟机搭建
查看>>
js数据类型以及原型分析
查看>>
laravel model relationship
查看>>
给老谢画的图(平面几何中的动点与最值问题)
查看>>
Step by step guide to set up master and slave machines on Windows
查看>>
理解java中的ThreadLocal 专题
查看>>
先有的资源,能看的速度看,不能看的,抽时间看。说不定那天就真的打不开了(转)...
查看>>
java开发一个应用的总结
查看>>
poj 1131进制转换
查看>>
android:layout_weight属性详解
查看>>
java随机生成字符串和校验
查看>>
[编程] TCP协议概述
查看>>
HashMap 原理?jdk1.7 与 1.8区别
查看>>
阿里云 Debian Linux 布署记录
查看>>
了解JavaScript 数组对象及其方法
查看>>
设置Tomcat的UTF-8编码
查看>>
Java基础5:抽象类和接口
查看>>
方法 属性 构造方法和包
查看>>