vuex 的使用(一)

一. 引入vuex

目錄:


image.png

index.js

//index.js

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state:{

  },
  actions:{

  },
  mutations:{

  },
  modules:{

  }

})

main.js 引入 store

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

二.vuex 三種取值方式

定義值

//index.js

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const add = {
  state:{
    userName: 'jack'
  },
  actions:{},
  mutations:{}
}
export default new Vuex.Store({
  state:{
    appName: 'admin'
  },
  actions:{

  },
  mutations:{

  },
  modules:{
    add
  }

})

1. 使用$store

<template>
    <div>
        <p>appName: {{ this.$store.state.appName }}</p>
        <p>appName:  {{ appName }}</p>
        <p>userName: {{userName}}</p>
    </div>
</template>
<script>
export default{
  computed:{
    appName(){
        return this.$store.state.appName
    },
    userName(){
        //取模塊里面的值
        return this.$store.state.add.userName
    }
  }
}
</script>

2. 使用vuex 提供的mapState 函數(shù)獲取

如果需要去模塊中的值溶推,第一個參數(shù)傳入模塊的名稱琴儿,或者是直接通過 “ . ” 操作符加模塊名稱缸废。

improt { mapState } from 'vuex'
<script>
export default{
  computed:{
    //數(shù)組的取值方式
//     ...mapState([
//        'appName'
//      ])

    //數(shù)組的 獲取模塊中的值
//     ...mapState('add',[
//        'appName'
//      ])

//    對象的取值方式     
      ...mapState({
        appName: state => state.appName,
        userName: state => state.add.userName
      })
  }
}
</script>

3.通過命名空間 模塊取值

首先需要在模塊中開啟命名空間

//分模塊   user.js

const state = {
  //
  urlName: 'www.baidu.com'
}


const actions = {
  //
}

const mutations = {
  //
}

export default {
  namespaced: true, //開啟命名空間
  state,
  actions,
  mutations
}


// index.js  ,引入user.js 模塊

import Vue from 'vue'
import Vuex from 'vuex'
import state from '@/store/state'
import actions from '@/store/actions'
import  mutations from '@/store/mutations'
import user from '@/store/module/user'

Vue.use(Vuex)

export default new Vuex.Store({

  state,
  mutations,
  actions,
  modules: {
    user
  }
})

模塊的取之又分兩種。

  • 使用 createNamespacedHelpers 函數(shù)彪置。
//index.vue

<template>
  <div >
    <p>
      urlName: {{urlName}}
    </p>

  </div>
</template>
<script>
import { createNamespacedHelpers } from 'vuex';
const { mapState } = createNamespacedHelpers('user'); //模塊的名稱
export default {
  name: 'home',
  data(){
    return {};
  },
  computed:{
    ...mapState({
      urlName: state => state.urlName
    })
  },
  methods:{

  }
}
</script>
  • 在使用mapState 取之時 第一個參數(shù)傳入模塊的名稱。
//index.vue

<template>
  <div >
    <p>
      urlName: {{urlName}}
    </p>

  </div>
</template>
<script>
import { mapState } from 'vuex';
export default {
  name: 'home',
  data(){
    return {};
  },
  computed:{
    ...mapState('user',{
      urlName: state => state.urlName
    })
  },
  methods:{

  }
}
</script>

三 纯衍、vuex 的 getter 部分

vuex 的 getter 部分相當(dāng)于組件的計算屬性任岸。當(dāng)他們的值改變的時候再榄,使用了他們的組件中的視圖和一些狀態(tài)都會發(fā)生變化

定義:

//getters.js  
const getters = {
  appNameAndVersion: (state) =>{
    // state 是同級目錄的state 對象
      return `${state.appName}V2.0`  
  }

}

export default getters;


//index.js
import Vue from 'vue'
import Vuex from 'vuex'
import state from '@/store/state'
import actions from '@/store/actions'
import  mutations from '@/store/mutations'
import user from '@/store/module/user'
import getters from '@/store/getters';

Vue.use(Vuex)

export default new Vuex.Store({
  state,
  getters,
  mutations,
  actions,
  modules: {
    user
  },
})

user.js

const state = {
  //
  urlName: 'www.baidu.com'
}

const getters = {
  urlNameApi: (state) =>{
    return `${state.urlName}/api`;
  }
}

const actions = {
  //
}

const mutations = {
  //
}

export default {
  namespaced: true,
  state,
  actions,
  getters,
  mutations
}



使用:
1.使用 $state

<template>
  <div >
    <p>
      appName: {{ this.$store.state.appName }}
      appNameAndVersion: {{ appNameAndVersion }}
    </p>

  </div>
</template>

<script>
import { mapState } from 'vuex';
export default {
  name: 'home',
  data(){
    return {};
  },
  computed:{

    ...mapState({
      appName: state => state.appName
    }),
    appNameAndVersion (){
      return this.$store.getters.appNameAndVersion
    },
    urlNameApi(){
        //獲取模塊中的值
        return this.$store.getters['user/urlNameApi']
    }
    
  },
  methods:{

  }
}
</script>

2.使用vuex 提供的 mapGetters 函數(shù)

<script>
import { mapState,mapGetters } from 'vuex';
export default {
 name: 'home',
 data(){
   return {};
 },
 computed:{

   ...mapState({
     appName: state => state.appName
   }),
    mapGetters([
       'appNameAndVersion'
   ]);
//   獲得模塊中的值  注意需要開啟命名空間
//     mapGetters('user',[
//        'urlNameApi'
//   ]);
 },
 methods:{

 }
}
</script>

如果不使用命名空間的話 可以直接獲取

將 user.js 中的 namespaced 命名空間關(guān)掉

const state = {
  //
  urlName: 'www.baidu.com'
}

const getters = {
  urlNameApi: (state) =>{
    return `${state.urlName}/api`;
  }
}

const actions = {
  //
}

const mutations = {
  //
}

export default {
 // namespaced: true,
  state,
  actions,
  getters,
  mutations
}
<script>
import { mapState,mapGetters } from 'vuex';
export default {
  name: 'home',
  data(){
    return {};
  },
  computed:{

    ...mapState({
      appName: state => state.appName
    }),

    //可以直接取模塊中的。
     mapGetters([
        'appNameAndVersion',
        'urlNameApi'
    ]);

  methods:{

  }
}
</script>


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末享潜,一起剝皮案震驚了整個濱河市困鸥,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌剑按,老刑警劉巖疾就,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異艺蝴,居然都是意外死亡猬腰,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進店門猜敢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來姑荷,“玉大人,你說我怎么就攤上這事缩擂∈竺幔” “怎么了?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵胯盯,是天一觀的道長懈费。 經(jīng)常有香客問我,道長博脑,這世上最難降的妖魔是什么憎乙? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮叉趣,結(jié)果婚禮上泞边,老公的妹妹穿的比我還像新娘。我一直安慰自己君账,他們只是感情好繁堡,可當(dāng)我...
    茶點故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布沈善。 她就那樣靜靜地躺著乡数,像睡著了一般椭蹄。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上净赴,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天绳矩,我揣著相機與錄音,去河邊找鬼玖翅。 笑死翼馆,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的金度。 我是一名探鬼主播应媚,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼猜极!你這毒婦竟也來了中姜?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤跟伏,失蹤者是張志新(化名)和其女友劉穎丢胚,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體受扳,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡携龟,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了勘高。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片峡蟋。...
    茶點故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖华望,靈堂內(nèi)的尸體忽然破棺而出层亿,到底是詐尸還是另有隱情,我是刑警寧澤立美,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布匿又,位于F島的核電站,受9級特大地震影響建蹄,放射性物質(zhì)發(fā)生泄漏碌更。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一洞慎、第九天 我趴在偏房一處隱蔽的房頂上張望痛单。 院中可真熱鬧,春花似錦劲腿、人聲如沸旭绒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽挥吵。三九已至重父,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間忽匈,已是汗流浹背房午。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留丹允,地道東北人郭厌。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓,卻偏偏與公主長得像雕蔽,于是被迫代替她去往敵國和親折柠。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,033評論 2 355