vuex for every one




having small component with single responsibitilties


when persone buys ticket we will emit a buy ticke tevent

and it tells to movie room one ticket sold

it will emit an event to parent movie compoenent

movie ticket event know user need whic h movie, which seeat, whichtimings required


and decreases the amount of available tickets using props

it might emit an event to app comonent

and t passes an event actial  ticket o shooping cardt item

this strucutre is fine for small and medium applications



it add cart o sshopomg cart

vuex  is state managment library for vue applicaitons


it serves as a centralized data store for all components in an application

instead of changing data directly we canuse vuex before changing data

every change in vuex, it can debuggable means log the things


 



person move to movie room 
boiler tempalte
vue init vueschool/webpack-template shopping-cart

vue init is the initilisation of vue

shopping-cart is the applicationname


add to card and check out 

https://github.com/vuejs/vuex


created will cll after the incstance created


import vuex from 'vuex'

vue.use(vuex)

export default new vuex.store({

state:{

products: []

},

getters:{

productsCount () {}

}

},

actions:{

fetchProducts () {}

}

mutations:{

setproducts(){}

}

)

vuex mutations 


single state change it woudl update products to array

fetch products make responsible for ajax call

setproducts => setting the products and update te products to array

mutitaoion -> simple -> just updating piece of state

actions can be complex it can never update the state

vuex always pass teh state in every paramaers in vuex

state meaans vaaible 

some additional paramaters also there it may be payload


so at invidual vue , there is no need od data object anymore because we are storing globally 

so required computed property 

it will return te product data






we cannout upate directly state

we have to cal mutations to update state

to update mutoation or commit mutation 

store.commit('state_name') with payload

store.commit('setproducts',products)

store steate to access the data

store mutation to update teh state

using vuedevtols plugin, we can inspect the vuex tracking


getters are tehe computed properties, we wol mostly at computed only instead of dta baraibles

similary like mutations, in getters functiion

ist parameter is state

second one is pauload

getters:{

availableproducts(state,getters)

{

return state.products.filter(product => product.inventor> 0);

}

}


computed:{

products () {

return  store.getters.availableproducts

}


}


vuex actions


everywhere ajax call -> we have to call that function if we write at specific vue page

insted that if we arite at vuex, without repeating code, we can cal fetch products


why we write fetch products api at created at indivudal vue, after create instance we have to call products

add to cart

if calls cart ist check product is avaialble or not then add to cart



store.commit => commit to state

if we cal actions -> store.dispatch



actions are asyncrhonous

we need to know when an action is done 

so by promise we knowthis


loading is true, once promise resolve loading is false


fetching products with time gap
actions







storea ccess from all components


if we inject at root ,it wil acess from all compoennts

main.js





so removing store  from product.list.vue

the global store is availab ewith unde $store
this.$sore.geters.availableproducts
actions example

addProductToCart(context,product)
{
if(product.inventory > 0)
{
const cartItem= contect.state.cart.find(item => item.id === product.id)
if(!cartItem)
{
context.commit('pusproducttocart', product)
} else {
context.commit('incrementitemquantitiy',cartItem)
}
context.commit('decrementProductInventory',product)
}
}
s



mutations







now we are callig from vue pages









vuex
https://chromewebstore.google.com/detail/vuejs-devtools/nhdogjmejiglipccpnnnanhbledajbpd



using above log we can revert the mutation 

cart items and total

checkout 



dynamic vuex getters

show all products, if product is already sold or no inventory disable add to cart functionality








vuex map helpers

some code repeating, then we can go with single functions 

mapstate







ex7 spread opearator
combine objects using spread operator




if we require so  many getters and setters in a vue page use mapgetters







spit vuex into multiple files

store/modules/cart.js
getters:{
cartproducts(stete,getteres,rootstatus)

}
rootstratus is 



name spaced vuex modules
https://github.com/vueschool/learn-vuex/blob/b213442087d67c8bba5238a7e89c6dfa351a60fe/src/store/modules/cart.jss

modules namspaced:true
 


write the prefix with name
ge

getters local module
rootgetters is ohter module
state is local nmodule
rootstate is other module

products/productomstpcl
vuexinside of vuex

root:true




https://github.com/vueschool/reusable-vuejs-components-with-slots


No comments:

Post a Comment

Event listening in react

 How we can listen to som eevents some envents fire like click or automatically user enters into input button , that is event on word type i...