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

Vuex模塊化編碼使用詳解

 更新時間:2025年10月22日 09:25:33   作者:yume_sibai  
本文介紹了 Vuex 的模塊化和命名空間功能,通過對 store.js 進行模塊劃分,提高代碼維護性和數(shù)據(jù)分類的明確性,并講解了在組件中讀取和操作模塊化 state、getters、actions、mutations 的具體方法

一、模塊化+命名空間

1.目的:讓代碼更好維護,讓多種數(shù)據(jù)分類更加明確。

2.修改store.js

        const countAbout = {
                namespaced:true,//開啟命名空間
                state:{x:1},
                mutations: { ... },
                actions: { ... },
                getters: {
                        bigSum(state){
                                return state.sum * 10
                        }
                }
        }
        const personAbout = {
                namespaced:true,//開啟命名空間
                state:{ ... },
                mutations: { ... },
                actions: { ... }
        }
        const store = new Vuex.Store({
                modules: {
                        countAbout,
                        personAbout
                }
         })

3.開啟命名空間后,組件中讀取state數(shù)據(jù):

        //方式一:自己直接讀取
        this.$store.state.personAbout.list
        //方式二:借助mapState讀取
        ...mapState('countAbout',['sum','school','subject']),

4.開啟命名空間后,組件中讀取getters數(shù)據(jù):

        //方式一:自己直接讀取
        this.$store.getters['personAbout/firstPersonName']
        //方式二:借助mapGetters讀取
        ...mapGetters('countAbout',['bigSum'])

5.開啟命名空間后,組件中調(diào)用dispatch

        //方式一:自己直接dispatch
        this.$store.dispatch('personAbout/addPersonWang',person)
        //方式二:借助mapActions:
        ...mapActions('countAbout',{incrementOdd:'incrementOdd',incrementWait:'incrementWait'})

6.開啟命名空間后,組件中調(diào)用commit

        //方式一:自己直接commit
        this.$store.commit('personAbout/PERSON_ADD',person)
        //方式二:借助mapMutations:
        ...mapMutations('countAbout',{increment:'INCREMENT',decrement:'DECREMENT'}),

二、代碼實現(xiàn)

將求和相關(guān)配置寫入countAbout.js中。

export default {
    namespaced:true,
    actions:{
        incrementOdd(context,value){
            if(context.state.sum % 2)
                context.commit('INCREMENTODD',value)
        },
        incrementWait(context,value){
            setTimeout(() => {
                context.commit('INCREMENTWAIT',value)
            }, 500);
        }
    },
    mutations:{
        INCREMENT(state,value){
            state.sum +=value
        },
        DECREMENT(state,value){
            state.sum -=value
        },
        INCREMENTODD(state,value){
            state.sum +=value
        },
        INCREMENTWAIT(state,value){
            state.sum +=value
        },
    },
    state:{
        sum:0,
        school:'清華大學(xué)',
        subject:'Vue',
    },
    getters:{
        bigSum(state){
            return state.sum*10
        }
    }
}

將求和相關(guān)配置寫入personAbout.js中。

import axios from 'axios'
import { nanoid } from 'nanoid'

export default {
    namespaced:true,
    actions:{
        personAddWang(context,value){
            if(value.name.indexOf('王') === 0){
                context.commit('PERSON_ADD',value)
            }
            else{
                alert('請?zhí)砑有胀醯娜?)
            }
        },
        personAddServer(context){
            axios.get('https://www.openapijs.com/api/random').then(
                Response=>{
                    context.commit('PERSON_ADD',{id:nanoid(),name:Response.data.data.poems.autho})
                },
                Error=>{
                    alert(Error.message)
                }
            )
        }
    },
    mutations:{
        PERSON_ADD(state,value){
            state.personList.unshift(value)
        }
    },
    state:{
        personList:[{
            id:'001',
            name:'張三'
        }]
    },
    getters:{
        firstPersonName(state){
            return state.personList[0].name
        }
    }
}

將countAbout和personAbout引入index.js中。

//引入Vue核心庫
import Vue from 'vue'
//引入Vuex
import Vuex from 'vuex'
import countAbout from './countAbout'
import personAbout from './personAbout'

//應(yīng)用Vuex插件
Vue.use(Vuex)


//創(chuàng)建并暴露store
export default new Vuex.Store({
    modules:{
        countAbout,
        personAbout
    }
})

模塊化后,在vue文件中不同的引用方法,在Count.js中使用map引用:

<template>
    <div class="main">
        <h1>這門課是{{ school }}的{{ subject }}課程</h1>
        <h2>當前求和為:{{ sum }}</h2>
        <h3>當前求和放大十倍為:{{ bigSum }}</h3>
        <h3 style="color: red;">當前人數(shù)為:{{ personList.length }}</h3>
        <select v-model.number="n">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
        </select>
        <button @click="INCREMENT(n)">+</button>
        <button @click="DECREMENT(n)">-</button>
        <button @click="incrementOdd(n)">當前為奇數(shù)再加</button>
        <button @click="incrementWait(n)">等會再加</button>
    </div>
</template>

<script>
    import {mapState,mapGetters,mapMutations,mapActions} from 'vuex'

    export default {
        name:'Count',
        data() {
			return {
                n:1
			}
		},
        methods: {
            ...mapMutations('countAbout',['INCREMENT','DECREMENT']),
            ...mapActions('countAbout',['incrementOdd','incrementWait']),
            
        },
        computed:{
            ...mapState('countAbout',['sum','school','subject']),
            ...mapState('personAbout',['personList']),
            ...mapGetters('countAbout',['bigSum'])

        }
    }
</script>

<style lang="less" scoped>
    button{
        margin-left: 5px;
    }
</style>

在Person.js中使用手動引用:

<template>
  <div>
    <h2>人員列表</h2>
    <h3 style="color: red;">Count組件的總和為:{{ sum }}</h3>
    <h3>列表第一個人名為:{{ firstPersonName }}</h3>
    <input type="text" v-model="person">
    <button @click="personAdd">點擊添加</button>
    <button @click="personAddWang">添加一個姓王的人</button>
    <button @click="personAddServer">隨機添加一個人</button>
    <ul>
        <li v-for="p in personList" :key="p.id">{{ p.name }}</li>
    </ul>
  </div>
</template>

<script>
    import { nanoid } from 'nanoid'
    
    export default {
        name:'Persons',
        data() {
            return {
                person:''
            }
        },
        methods: {
            personAdd(){
                const obj = {id:nanoid(),name:this.person}
                this.$store.commit('personAbout/PERSON_ADD',obj)
                this.person=''
            },
            personAddWang(){
                const obj = {id:nanoid(),name:this.person}
                this.$store.dispatch('personAbout/personAddWang',obj)
                this.person=''
            },
            personAddServer(){
                this.$store.dispatch('personAbout/personAddServer')
            }
        },
        computed:{
            personList(){
                return this.$store.state.personAbout.personList
            },
            sum(){
                return this.$store.state.countAbout.sum
            },
            firstPersonName(){
                return this.$store.getters['personAbout/firstPersonName']
            }
        }
    }
</script>

<style lang="less">
    ul{
        font-size: 18px;
        li{
            padding-top: 5px;
        }
    }
</style>

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue3.2?setup語法糖及Hook函數(shù)基本使用

    Vue3.2?setup語法糖及Hook函數(shù)基本使用

    這篇文章主要為大家介紹了Vue3.2?setup語法糖及Hook函數(shù)基本使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • webpack配置導(dǎo)致字體圖標無法顯示的解決方法

    webpack配置導(dǎo)致字體圖標無法顯示的解決方法

    下面小編就為大家分享一篇webpack配置導(dǎo)致字體圖標無法顯示的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • vue?LogicFlow更多配置選項示例詳解

    vue?LogicFlow更多配置選項示例詳解

    這篇文章主要為大家介紹了vue?LogicFlow更多配置選項詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • vue3前端實現(xiàn)微信支付詳細步驟

    vue3前端實現(xiàn)微信支付詳細步驟

    這篇文章主要給大家介紹了vue3前端實現(xiàn)微信支付的詳細步驟,隨著移動端的普及和互聯(lián)網(wǎng)購買需求的增加,微信支付在電商領(lǐng)域中發(fā)揮著越來越重要的作用,文中給出了詳細的代碼示例,需要的朋友可以參考下
    2023-11-11
  • vue引用外部JS并調(diào)用JS文件中的方法實例

    vue引用外部JS并調(diào)用JS文件中的方法實例

    我們在做vue項目時,經(jīng)常會需要引入js,下面這篇文章主要給大家介紹了關(guān)于vue引用外部JS并調(diào)用JS文件中的方法的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-02-02
  • Vue實現(xiàn)父子組件傳值其實不難

    Vue實現(xiàn)父子組件傳值其實不難

    這篇文章主要介紹了Vue實現(xiàn)父子組件傳值其實不難問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Vue3+script setup+ts+Vite+Volar搭建項目

    Vue3+script setup+ts+Vite+Volar搭建項目

    本文主要介紹了Vue3+script setup+ts+Vite+Volar搭建項目,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Vue3項目打包并部署到Nginx實踐

    Vue3項目打包并部署到Nginx實踐

    本文介紹了如何安裝Nginx并部署Vue項目,首先從官網(wǎng)下載Nginx并啟動服務(wù),然后使用Vite構(gòu)建Vue項目,將構(gòu)建好的dist文件夾部署到Nginx目錄下,并修改nginx.conf文件以防止刷新頁面時出現(xiàn)404錯誤,最后,總結(jié)了刷新頁面空白問題的解決方法
    2026-01-01
  • Vue監(jiān)聽使用方法和過濾器實現(xiàn)

    Vue監(jiān)聽使用方法和過濾器實現(xiàn)

    這篇文章主要介紹了Vue監(jiān)聽使用方法和過濾器實現(xiàn),過濾器為頁面中數(shù)據(jù)進行強化,具有局部過濾器和全局過濾器
    2022-06-06
  • 解決Vue打包之后文件路徑出錯的問題

    解決Vue打包之后文件路徑出錯的問題

    下面小編就為大家分享一篇解決Vue打包之后文件路徑出錯的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03

最新評論

昔阳县| 彭阳县| 松潘县| 涿鹿县| 泰兴市| 吴桥县| 玉山县| 临西县| 神木县| 交口县| 绵竹市| 宁海县| 新宁县| 开封县| 临泽县| 张家界市| 九台市| 梁平县| 恭城| 萝北县| 韶关市| 凤城市| 霍邱县| 泸溪县| 谷城县| 民和| 渭源县| 韶山市| 收藏| 濉溪县| 方城县| 盐城市| 天峨县| 儋州市| 六安市| 南雄市| 水城县| 昌宁县| 阜宁县| 隆德县| 南陵县|