国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

nuxt框架中對vuex進行模塊化設置的實現方法

2019-11-19 10:51:53
字體:
來源:轉載
供稿:網友
1.Nuxt里怎么使用vuex?

Nuxt.js 內置引用了 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 文件會被轉換成為狀態樹指定命名的子模塊 (當然,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中存放數量)

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) { //數量 state.num= num }};
3.在vue文件中獲取vuex的數據,調用mutation中的方法修改數據
<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',參數) //使用子模塊的mutation中的方法 this.$store.commit(‘文件名/方法名',參數) }, setPrice(){ this.$store.commit('child1/getPrice',參數) //使用子模塊的mutation中的方法 }, },
補充: 使用子模塊的action: this.$store.dispatch(‘文件名/變量名')

參考鏈接:
http://m.survivalescaperooms.com/article/169502.htm
http://m.survivalescaperooms.com/article/169504.htm

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丹阳市| 宝丰县| 公安县| 富裕县| 通山县| 云安县| 高阳县| 凤山市| 高邮市| 瑞丽市| 安泽县| 汶川县| 呈贡县| 贵定县| 甘肃省| 巴里| 东辽县| 克山县| 新巴尔虎左旗| 稷山县| 来宾市| 普安县| 商城县| 淳安县| 扶风县| 渑池县| 香港 | 神池县| 通辽市| 武川县| 汤原县| 北票市| 余干县| 湘西| 呼玛县| 鄂伦春自治旗| 巩留县| 淳安县| 长岛县| 怀安县| 巴马|