鍍金池/ 問(wèn)答/HTML/ vuex中state.js action.js type.js mutation

vuex中state.js action.js type.js mutations.js 中幾個(gè)想不通的問(wèn)題

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)這是最好的方法么?

回答
編輯回答
情皺

1、關(guān)于type.js,確實(shí)只是一個(gè)規(guī)范化的管理方式,把所有的mutations的操作,都定義在type.js里面。
2、存儲(chǔ)方式,目前項(xiàng)目中使用了vuex-persistedstate 這個(gè)插件。默認(rèn)使用的是localStorage。當(dāng)然你可以選擇SessionSorage以及cookie。這些選擇都是基于你了解了他們之間的區(qū)別,以及根據(jù)項(xiàng)目需求來(lái)選擇的。沒(méi)有最好,只有最合適把

2018年9月7日 00:33
編輯回答
尛曖昧

說(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,如果有更多或者更好的辦法的話,麻煩告知我:-)。

2018年2月11日 23:21