最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

nuxt框架中對vuex進行模塊化設置的實現(xiàn)方法

 更新時間:2019年09月06日 11:55:08   作者:程南風  
這篇文章主要介紹了nuxt框架中對vuex進行模塊化設置的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
1.Nuxt里怎么使用vuex?

Nuxt.js 內(nèi)置引用了 vuex 模塊,所以不需要額外安裝。

Nuxt.js 會嘗試找到應用根目錄下的 store 目錄,如果該目錄存在,它將做以下的事情:
1.1> 引用 vuex 模塊
1.2> 將 vuex 模塊 加到 vendors 構建配置中去
1.3> 設置 Vue 根實例的 store 配置項

Nuxt.js 支持兩種使用 store 的方式:
普通方式: store/index.js 返回一個 Vuex.Store 實例
模塊方式: store 目錄下的每個.js 文件會被轉(zhuǎn)換成為狀態(tài)樹指定命名的子模塊 (當然,index 是根模塊)

2.Nuxt中怎么對vuex進行模塊化設置?

2.1> 例如 -- 設置index.js為根模塊,child1.js與child2.js兩個子模塊

2.2> 在store/index.js 中不需要返回 Vuex.Store 實例,可以直接將 state、mutations 和 actions 暴露出來:
(以下為例:index中存儲商品總價,child1中存儲單價,child2中存放數(shù)量)

export const state = () =>({
 totalPrice:0,
});
export const mutations = {
 totalPrice (state) { //總價
 // state.totalPrice = state.num*state.price 錯誤方式:使用子模塊的state,應該在變量名前加上文件名,如下
 state.totalPrice = state.child1.price*state.child2.num //正確方式
 }
};

子模塊中同樣直接將 state、mutations 和 actions 暴露出來:

child1.js1
export const state = () =>({
 price:10, //單價
});
export const mutations = {
 getPrice(state,price) { 
 state.price= price
 }
};
child2.js2
export const state = () =>({
 num:5,
});
export const mutations = {
 getNum(state,num) { //數(shù)量
 state.num= num
 }
};
3.在vue文件中獲取vuex的數(shù)據(jù),調(diào)用mutation中的方法修改數(shù)據(jù)
<script>
export default {
 name : 'test',
 data() {
 return {
 totalPrice:this.$store.state.totalPrice, //取index.js(根模塊)中的值
 num:this.$store.state.child2.num,  //取子模塊中的值
 price:this.$store.state.child1.price, //取子模塊中的值
 }
 },
}
</script>
 methods: {
 setTotalPrice(){
 this.$store.commit('totalPrice')
 },
 setNum(){
 this.$store.commit('child2/getNum',參數(shù)) //使用子模塊的mutation中的方法 this.$store.commit(‘文件名/方法名',參數(shù))
 },
 setPrice(){
 this.$store.commit('child1/getPrice',參數(shù)) //使用子模塊的mutation中的方法
 },
 },
補充: 使用子模塊的action: this.$store.dispatch(‘文件名/變量名')

參考鏈接:
http://www.fzitv.net/article/169502.htm
http://www.fzitv.net/article/169504.htm

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

哈密市| 蒙城县| 金阳县| 镇康县| 肇源县| 东台市| 边坝县| 新乡县| 蒲江县| 贵溪市| 贵溪市| 兴国县| 喀喇| 八宿县| 石柱| 从化市| 司法| 南雄市| 镇康县| 定边县| 金湖县| 无锡市| 南昌市| 石泉县| 北海市| 威信县| 南充市| 泰来县| 汶上县| 南和县| 鄂尔多斯市| 黄冈市| 文成县| 鄯善县| 建宁县| 辽阳市| 广平县| 宝鸡市| 汝城县| 泰兴市| 志丹县|