state.js:
const state={
article:localStorage["article"]?JSON.parse(localStorage["article"]): [],
collections:localStorage["collections"]?JSON.parse(localStorage["collections"]): [],
carts:localStorage["carts"]?JSON.parse(localStorage["carts"]): [],
orders:localStorage["orders"]?JSON.parse(localStorage["orders"]): [],
todos:localStorage["todos"]?JSON.parse(localStorage["todos"]): [],
address:localStorage["address"]?JSON.parse(localStorage["address"]): [],
nowIndex:0,
arr:[1,2,3,4,5]
}
export default state
action.js:
const actions={
//購(gòu)物車
setCart({commit},data){
commit('SET_CARTS',data)
},
//文章收藏
setArticle({commit},data){
commit('SET_ARTICLE',data)
},
//商品收藏
setGoods({commit},data){
commit('SET_GOODS',data)
},
//地址
setAddress({commit},data){
commit('SET_ADDRESS',data)
},
//訂單
setOrders({commit},data){
commit('SET_ORDERS',data)
}
}
export default actions
type.js:
export const SET_CARTS = 'SET_CARTS' //加入購(gòu)物車
export const SET_ARTICLE ='SET_ARTICLE' //文章收藏
export const SET_GOODS='SET_GOODS' //商品收藏
export const SET_ORDERS='SET_ORDERS' //訂單
export const SET_ADDRESS='SET_ADDRESS' //地址
import state from './state'
import * as type from './type.js'
import { MessageBox } from 'mint-ui';
const matutaions={
//購(gòu)物車
[type.SET_CARTS](state,data){
state.carts.push(data)
localStorage.setItem("carts",JSON.stringify(state.carts));
},
//文章
[type.SET_ARTICLE](state,data){
state.article.push(data)
localStorage.setItem("article",JSON.stringify(state.article));
},
//商品
[type.SET_GOODS](state,data){
state.collections.push(data)
localStorage.setItem("collections",JSON.stringify(state.collections));
},
//訂單
[type.SET_ORDERS](state,data){
state.orders.push(data)
localStorage.setItem("orders",JSON.stringify(state.orders));
},
//地址
[type.SET_ADDRESS](state,data){
state.address.push(data)
localStorage.setItem("address",JSON.stringify(state.address));
},
//文章刪除
del:(state,index)=>{
MessageBox.confirm('確定取消收藏該文章么?').then(action=>{
state.article.splice(index,1)
localStorage.setItem("article",JSON.stringify(state.article));
})
},
//商品刪除
cancel:(state,index)=>{
MessageBox.confirm('確定取消收藏該商品么?').then(action=>{
state.collections.splice(index,1)
localStorage.setItem("collections",JSON.stringify(state.collections));
})
},
laji:(state,index)=>{
MessageBox.confirm('確定刪除收貨地址么?').then(action=>{
state.address.splice(index,1)
localStorage.setItem("address",JSON.stringify(state.address));
})
},
//購(gòu)物車刪除
shanchu:(state,index)=>{
MessageBox.confirm('確定刪除該商品么?').then(action=>{
state.carts.splice(index,1)
localStorage.setItem("carts",JSON.stringify(state.carts));
})
},
//訂單刪除
odefault:(state,index)=>{
MessageBox.confirm('確定刪除該訂單么?').then(action=>{
state.orders.splice(index,1)
localStorage.setItem("orders",JSON.stringify(state.orders));
})
},
//數(shù)量加
add(state,index){
state.carts[index].value++
},
//數(shù)量減
reduce(state,index){
state.carts[index].value==1?state.carts[index].value=1: state.carts[index].value--
},
settlement:(state,data)=>{
state.carts=[];
localStorage.setItem("carts",JSON.stringify(state.carts));
},
}
export default matutaions
上述代碼源于github一個(gè)項(xiàng)目,我是下載下來(lái)自己研究。在這里有幾個(gè)問(wèn)題請(qǐng)教大家一下。
1、在vuex 整個(gè)結(jié)構(gòu)中,type.js 扮演的什么角色,在學(xué)習(xí)vuex的時(shí)候沒(méi)有見(jiàn)到過(guò)這種寫法,為什么要這么寫?必須要這么寫么?
2、vuex中action.js 有一些方法,但是不是全部,就是為什么//地址
[type.SET_ADDRESS](state,data){
state.address.push(data)
localStorage.setItem("address",JSON.stringify(state.address));
},
//文章刪除
del:(state,index)=>{
MessageBox.confirm('確定取消收藏該文章么?').then(action=>{
state.article.splice(index,1)
localStorage.setItem("article",JSON.stringify(state.article));
})
},
同樣是vuex里面的方法,有的方法是用上面的 [type.SET_ADDRESS]寫法,有的直接就可以像del這么使用,為什么要寫兩種呢?
3、vuex 一刷新瀏覽器然后vuex里面的state數(shù)據(jù)就消失了,我查到了localstorage,請(qǐng)問(wèn)這是最好的方法么?
說(shuō)一下我個(gè)人的理解吧。
關(guān)于type.js
簡(jiǎn)單來(lái)講,type.js只是一個(gè)引用,或者說(shuō)字符串的映射。
type.js把這些string集中在一起使得你可以很直觀的看到你的模塊或者app具有哪些狀態(tài)以及狀態(tài)變化。
這里是官方的解釋: https://vuex.vuejs.org/zh/gui...。
// type.js
export const GET_LIST = 'GET_ORDER_LIST'
// action.js
[types.GET_LIST](...) {...} // 實(shí)際上等價(jià)于直接使用string: GET_ORDER_LIST(...) {...}
至于第二個(gè)問(wèn)題,我只能說(shuō)是代碼的規(guī)范問(wèn)題了,不使用type.js當(dāng)然可以,只要你能保證action的name能夠不沖突就行了。但是個(gè)人還是比較建議同時(shí)只使用一種,要么都用type,要么都不用。
關(guān)于數(shù)據(jù)持久化,參考目前的一些插件的話,也都是用的localStorage,我自己也是用的localStorage和sessionStorage,如果有更多或者更好的辦法的話,麻煩告知我:-)。
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。