久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      vue里store的用法是什么

      在vue中,store用于管理狀態(tài)、共享數(shù)據(jù)以及在各個組件之間管理外部狀態(tài),store是vuex應(yīng)用的核心,也就是一個容器,包含著應(yīng)用中大部分的狀態(tài),更改store中的狀態(tài)唯一方法是提交mutation。

      vue里store的用法是什么

      本文操作環(huán)境:windows10系統(tǒng)、Vue2.9.6版,DELL G3電腦。

      vue里store的用法是什么

      Vuex 是一個專為 Vue.js 應(yīng)用程序開發(fā)的狀態(tài)管理模式。它采用集中式存儲管理應(yīng)用的所有組件的狀態(tài),并以相應(yīng)的規(guī)則保證狀態(tài)以一種可預(yù)測的方式發(fā)生變化。

      每一個 Vuex 應(yīng)用的核心就是 store(倉庫)?!皊tore”基本上就是一個容器,它包含著你的應(yīng)用中大部分的狀態(tài) (state)。Vuex 和單純的全局對象有以下兩點(diǎn)不同:

      Vuex 的狀態(tài)存儲是響應(yīng)式的。當(dāng) Vue 組件從 store 中讀取狀態(tài)的時候,若 store 中的狀態(tài)發(fā)生變化,那么相應(yīng)的組件也會相應(yīng)地得到高效更新。

      你不能直接改變 store 中的狀態(tài)。改變 store 中的狀態(tài)的唯一途徑就是顯式地提交 (commit) mutation。這樣使得我們可以方便地跟蹤每一個狀態(tài)的變化,從而讓我們能夠?qū)崿F(xiàn)一些工具幫助我們更好地了解我們的應(yīng)用。

      store 的核心概念

      state 表示了 store 中的狀態(tài),類似于 vue 實(shí)例中的 data 屬性。

      Mutation

      更改 Vuex 的 store 中的狀態(tài)的唯一方法是提交 mutation。

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

      Action

      Action 類似于 mutation,不同在于:

      Action 提交的是 mutation,而不是直接變更狀態(tài)。

      Action 可以包含任意異步操作。

      一個示例

      const store = new Vuex.Store({   state: {     count: 0   },   mutations: {     increment (state) {       state.count++     }   },   actions: {     increment (context) {       context.commit('increment')     }   } })

      store 的用法

      使用 store 之前, 先要安裝 vuex :

      npm install vuex

      安裝 Vuex 之后,讓我們來創(chuàng)建一個 store。創(chuàng)建過程直截了當(dāng)——僅需要提供一個初始 state 對象和一些 mutation。

      新建 store 文件夾,再新建 index.js 文件:

      import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({     state: {         count: 0     },     mutations: {         increment(state){             state.count++;         }     } })

      為了在 Vue 組件中訪問 this.$store property,你需要為 Vue 實(shí)例提供創(chuàng)建好的 store。Vuex 提供了一個從根組件向所有子組件,以 store 選項(xiàng)的方式“注入”該 store 的機(jī)制。

      也就是在 main.js 文件中導(dǎo)入,并注冊到 vue 根實(shí)例中:

      import store from './store' ... new Vue({     el: "#app",     store: store, ...

      然后就可以在任意一個 vue 組件的 methods 方法屬性下通過 store.commit('increment')來調(diào)用:

              ...         methods:{             increment:function(){                 this.$store.commit("increment");                 console.log(this.$store.state.count);             },             ...

      效果如下:

      vue里store的用法是什么

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號