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

vue基礎(chǔ)入門之vuex安裝與使用

 更新時間:2021年08月18日 12:40:07   作者:小黃雞1992  
vuex是一個專為vue.js應(yīng)用程序開發(fā)的 狀態(tài)管理模式,它采用集中式存儲管理應(yīng)用的所有的狀態(tài),并以相應(yīng)的規(guī)則保證狀態(tài)以一種可預(yù)測的方式發(fā)生變化,這篇文章主要給大家介紹了關(guān)于vue入門之vuex安裝與使用的相關(guān)資料,需要的朋友可以參考下

本教程為入門教程,如有錯誤,請各位前端大佬指出。

1.什么是vuex

 Vuex 是一個專為 Vue.js 應(yīng)用程序開發(fā)的狀態(tài)管理模式。它采用集中式存儲管理應(yīng)用的所有組件的狀態(tài),并以相應(yīng)的規(guī)則保證狀態(tài)以一種可預(yù)測的方式發(fā)生變化.詳細(xì)介紹可以參照官網(wǎng)文檔vuex.vuejs.org/zh/

下面簡單介紹vuex

2.安裝和引入

先安裝vuex。

npm install vuex --save

在main.js中引入后即可使用。

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
//vuex使用
import Vuex from 'vuex'

Vue.use(Vuex)
const store = new Vuex.Store({
    state: {
        //全局變量
        count: 31231
    }
})

Vue.config.productionTip = false
    /* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    //vuex必須加入
    store,
    components: { App },
    template: '<App/>'
})

3.vuex的使用

<template>
<div>
      老大有{{showData}}
      <HelloWorld2/>
</div>
</template>

<script>
import HelloWorld2 from './HelloWorld2'
import son from './son'

export default {
name: 'HelloWorld',
data () {
  return {
       message2:"",
       cou
    }
},
components:{
  HelloWorld2,
  son
},computed: {
  showData(){
    return this.$store.state.count;
  }
}
}

</script>
<template>
<div>
老二有{{$store.state.count}}
</div>
</template>

<script>
export default {
name: 'HelloWorld2',
data() {
      return {
      }
    }
}
</script>

4.流程介紹

如圖當(dāng)沒有使用vuex時流程為: view->actions->state->view

使用了vuex后流程為vuecomponent->(dispatch)actions->(commit)mutations->(mutate)state->(render)->vuecomponent

5.mutation

狀態(tài)更改,更改 Vuex 的 store 中的狀態(tài)的唯一方法是提交mutation。Vuex 中的 mutation 非常類似于事件:每個 mutation 都有一個字符串的事件類型 (type)和一個回調(diào)函數(shù) (handler)。這個回調(diào)函數(shù)就是我們實際進行狀態(tài)更改的地方,并且它會接受 state 作為第一個參數(shù)。

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
//vuex使用
import Vuex from 'vuex'

Vue.use(Vuex)
const store = new Vuex.Store({
    state: {
        //全局變量
        count: 31231
    },
    //更改狀態(tài)方法
    mutations: {
        //state為上面的state
        addData(state) {
            // 變更狀態(tài)
            state.count++
        }
    }
})

Vue.config.productionTip = false
    /* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    //vuex必須加入
    store,
    components: { App },
    template: '<App/>'
})

然后執(zhí)行更改

<template>
<div>
      老大有{{showData}}
      <HelloWorld2/>
      <button type = "button" v-on:click = "changeData">  修改按鈕    </button>
</div>
</template>

<script>
import HelloWorld2 from './HelloWorld2'
import son from './son'

export default {
name: 'HelloWorld',
data () {
  return {
       message2:"",
    }
},
components:{
  HelloWorld2,
  son
},computed: {
  showData(){
    return this.$store.state.count;
  }
},
methods: {
  //執(zhí)行更改
  changeData(event){
      this.$store.commit("addData");
  }
}
}

</script>

6.getters過濾

可以限制mutation 比如小于0就不能減少了

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
//vuex使用
import Vuex from 'vuex'

Vue.use(Vuex)
const store = new Vuex.Store({
    state: {
        //全局變量
        count: 0
    },
    //更改狀態(tài)方法
    mutations: {
        //state為上面的state
        addData(state) {
            // 變更狀態(tài)
            state.count++
        }
    },
    //過濾
    getters: {
        getState(state) {
            if (state.count >= 5) {
                return 5
            } else {
                return state.count
            }
        }
    }
})

Vue.config.productionTip = false
    /* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    //vuex必須加入
    store,
    components: { App },
    template: '<App/>'
})

調(diào)用時

<template>
<div>
老二有{{$store.getters.getState}}
</div>
</template>

<script>
export default {
name: 'HelloWorld2',
data() {
      return {
      }
    }
}
</script>

7.Action--異步處理

Action 類似于 mutation,不同在于:

Action 提交的是 mutation,而不是直接變更狀態(tài)。 Action 可以包含任意異步操作。 mutation只能同步處理
main.js。示例如下:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
//vuex使用
import Vuex from 'vuex'

Vue.use(Vuex)
const store = new Vuex.Store({
    state: {
        //全局變量
        count: 0
    },
    //更改狀態(tài)方法
    mutations: {
        //state為上面的state
        addData(state) {
            // 變更狀態(tài)
            state.count++
        }
    },
    //過濾
    getters: {
        getState(state) {
            if (state.count >= 5) {
                return 5
            } else {
                return state.count
            }
        }
    },
    actions: {
        //action觸發(fā)的mutations方法 優(yōu)勢是異步處理
        addData(context) {
            //模擬異步
            setTimeout(() => {
                context.commit('addData')
            }, 1000)
        }
    }
})

Vue.config.productionTip = false
    /* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    //vuex必須加入
    store,
    components: { App },
    template: '<App/>'
})

在發(fā)送時 應(yīng)該調(diào)用action。

<template>
<div>
      老大有{{showData}}
      <HelloWorld2/>
      <button type = "button" v-on:click = "changeData">  修改按鈕    </button>
</div>
</template>

<script>
import HelloWorld2 from './HelloWorld2'
import son from './son'

export default {
name: 'HelloWorld',
data () {
  return {
       message2:"",
    }
},
components:{
  HelloWorld2,
  son
},computed: {
  showData(){
    return this.$store.getters.getState;
  }
},
methods: {
  //執(zhí)行更改
  changeData(event){
      //操作mutations方法
      //this.$store.commit("addData");
      //應(yīng)該操作action而不是action觸發(fā)的mutations方法
      this.$store.dispatch("addData");

  }
}
}

</script>

8.Module

由于使用單一狀態(tài)樹,應(yīng)用的所有狀態(tài)會集中到一個比較大的對象。當(dāng)應(yīng)用變得非常復(fù)雜時,store 對象就有可能變得相當(dāng)臃腫。

為了解決以上問題,Vuex 允許我們將 store 分割成模塊(module)。每個模塊擁有自己的 state、mutation、action、getter、甚至是嵌套子模塊——從上至下進行同樣方式的分割:

如路由可以分割文件 不在main.js中放入vuex 新建store/index.js

//vuex使用
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        //全局變量
        count: 0
    },
    //更改狀態(tài)方法
    mutations: {
        //state為上面的state
        addData(state) {
            // 變更狀態(tài)
            state.count++
        }
    },
    //過濾
    getters: {
        getState(state) {
            if (state.count >= 5) {
                return 5
            } else {
                return state.count
            }
        }
    },
    actions: {
        //action觸發(fā)的mutations方法 優(yōu)勢是異步處理
        addData(context) {
            //模擬異步
            setTimeout(() => {
                context.commit('addData')
            }, 1000)
        }
    }
})

修改main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'


Vue.config.productionTip = false
    /* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    //vuex必須加入
    store,
    components: { App },
    template: '<App/>'
})

我們還能把main.js中的state拿出 新建store/state.js

export default {
    count: 0
}

然后index.js可以改成

//vuex使用
import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'

Vue.use(Vuex)

export default new Vuex.Store({
    state: state,
    //更改狀態(tài)方法
    mutations: {
        //state為上面的state
        addData(state) {
            // 變更狀態(tài)
            state.count++
        }
    },
    //過濾
    getters: {
        getState(state) {
            if (state.count >= 5) {
                return 5
            } else {
                return state.count
            }
        }
    },
    actions: {
        //action觸發(fā)的mutations方法 優(yōu)勢是異步處理
        addData(context) {
            //模擬異步
            setTimeout(() => {
                context.commit('addData')
            }, 1000)
        }
    }
})

總結(jié)

到此這篇關(guān)于vue入門之vuex安裝與使用的文章就介紹到這了,更多相關(guān)vuex安裝與使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue3.x?的shallowReactive?與?shallowRef?使用場景分析

    vue3.x?的shallowReactive?與?shallowRef?使用場景分析

    在Vue3.x中,`shallowReactive`和`shallowRef`是用于創(chuàng)建淺層響應(yīng)式數(shù)據(jù)的API,它們與`reactive`和`ref`類似,本文介紹vue3.x??shallowReactive?與?shallowRef的使用場景,感興趣的朋友一起看看吧
    2025-02-02
  • 如何解決Vue請求接口出現(xiàn)跨域問題Access-Control-Allow-Origin

    如何解決Vue請求接口出現(xiàn)跨域問題Access-Control-Allow-Origin

    這篇文章主要介紹了如何解決Vue請求接口出現(xiàn)跨域問題Access-Control-Allow-Origin,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • SpringBoot+Vue3實現(xiàn)上傳文件功能

    SpringBoot+Vue3實現(xiàn)上傳文件功能

    這篇文章主要介紹了SpringBoot+Vue3實現(xiàn)上傳文件功能,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-01-01
  • vue中使用scrollTo無效的解決方法

    vue中使用scrollTo無效的解決方法

    本文主要介紹了vue中使用scrollTo無效的解決方法,想要使用scrollTo使當(dāng)前網(wǎng)頁滾動到指定位置,本文就來解決一下,具有一定的 參考價值,感興趣的可以了解一下
    2023-08-08
  • vue下拉列表的兩種實現(xiàn)方式比較

    vue下拉列表的兩種實現(xiàn)方式比較

    這篇文章主要介紹了vue下拉列表的兩種實現(xiàn)方式比較,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Vue方法與事件處理器詳解

    Vue方法與事件處理器詳解

    這篇文章主要為大家詳細(xì)介紹了Vue方法與事件處理器,,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • vue的狀態(tài)更新方式(異步更新解決)

    vue的狀態(tài)更新方式(異步更新解決)

    這篇文章主要介紹了vue的狀態(tài)更新方式(異步更新解決),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue引入vuetify框架你需要知道的幾點知識

    Vue引入vuetify框架你需要知道的幾點知識

    這篇文章主要介紹了Vue引入vuetify框架你需要知道的幾點知識,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • vue中wangEditor的使用及回顯數(shù)據(jù)獲取焦點的方法

    vue中wangEditor的使用及回顯數(shù)據(jù)獲取焦點的方法

    最近在寫vue的項目中,遇到一個需求,點擊編輯,顯示彈框,在彈框中的富文本編輯器中編輯自定義文本樣式,可以上傳圖片并回顯。接下來通過本文給大家介紹vue中wangEditor的使用及回顯數(shù)據(jù)獲取焦點的問題,一起看看吧
    2021-09-09
  • solid.js響應(yīng)式createSignal 源碼解析

    solid.js響應(yīng)式createSignal 源碼解析

    這篇文章主要為大家介紹了solid.js響應(yīng)式createSignal 源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09

最新評論

肥西县| 尉氏县| 利辛县| 比如县| 桑日县| 霍州市| 元谋县| 遵化市| 青神县| 松溪县| 岚皋县| 齐齐哈尔市| 霍州市| 周宁县| 平昌县| 扎鲁特旗| 泌阳县| 女性| 巴彦淖尔市| 裕民县| 廉江市| 丹巴县| 上栗县| 白沙| 本溪市| 同江市| 小金县| 虹口区| 建瓯市| 新泰市| 扎鲁特旗| 玛多县| 若羌县| 屯留县| 苍南县| 巴塘县| 海城市| 潍坊市| 海原县| 西华县| 阿克苏市|