Vue 學(xué)習(xí)筆記4(77-87)

Vue 學(xué)習(xí)筆記4(77-87)

學(xué)習(xí)地址:https://ke.qq.com/course/279700

目錄:

  • 01-01 git下載代碼
  • 02-27 Vue2.x
  • 28-39 路由精講
  • 40-49 Vue-Cli3.0
  • 50-64 Vue2.x-實(shí)戰(zhàn)項(xiàng)目(個(gè)人博客)
  • 65-76 Vue2.x-實(shí)戰(zhàn)項(xiàng)目(用戶管理)
  • 77-87 Vuex 核心概念如下

vuex

01 Vuex-成果展示及項(xiàng)目搭建

02 Vuex-一個(gè)簡(jiǎn)單的Vue APP

父子傳值

<product-list-one :products='products'></product-list-one>
props: ['products'],

State

03 Vuex-搭建Vuex中央狀態(tài)管理, 04 Vuex-使用computed獲取store數(shù)據(jù)

Vuex

Centralized State Management for Vue.js.

src 目錄下新建 store 文件夾坊夫,下面新建 index.js 文件

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

Vue.use(Vuex)

export const store = new Vuex.Store({
  state:{
    products:[
      {name:'馬云',price:'200'},
      {name:'馬化騰',price:'140'},
      {name:'馬冬梅',price:'20'},
      {name:'馬蓉',price:'10'},
    ]
  }
}) 

main.js 中引入

import {store} from './store'
new Vue({
  store,
  ...
}

組建中使用,computed 屬性獲取 store 中數(shù)據(jù)

<li v-for="product in products" :key='product'>
  <span class="name">{{product.name}}</span>
  <span class="price">${{product.price}}</span>
</li>

<script>
export default{
  computed: {
    products(){
      return this.$store.state.products
    }
  },
}
</script>

Getter

05 Vuex-Getters

index.js 文件中怎披,新增 getters 屬性辰斋,里面放其他組件可以調(diào)用的方法

export const store = new Vuex.Store({
  state:{
    products:[
      {name:'馬云',price:'200'},
      {name:'馬化騰',price:'140'},
      {name:'馬冬梅',price:'20'},
      {name:'馬蓉',price:'10'},
    ]
  },
  getters:{
    saleProducts:(state)=>{
      var saleProducts = state.products.map(product =>{
        return{
          name: "**" + product.name + "**" ,
          price: product.price / 2
        }
      });
      return saleProducts;
    }
  }
}) 

組件對(duì) store 中方法的調(diào)用

<li v-for="product in saleProducts" :key='product'>
  <span class="name">{{product.name}}</span>
  <span class="price">${{product.price}}</span>
</li>

<script>
export default{
  computed: {
    saleProducts(){
      return this.$store.getters.saleProducts;
    }
  },
}
</script>

Mutation

06 Vuex-Mutations

谷歌瀏覽器添加插件Vue.js devtools可以跟蹤vuex狀態(tài)

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

store/index.js 中添加 mutations 屬性

  mutations:{
    reducePriceV:state=>{
      state.products.forEach(product =>{
        product.price -= 1;
      })
    }
  }

組建中調(diào)用方法 reducePriceV

<button @click="reducePrice">降價(jià)</button>
<script>
export default {
  methods: {
    reducePrice(){
      this.$store.commit('reducePriceM') // mutations中的方法
    }
  },
}
</script>

Action

07 Vuex-Actions

Action 類似于 mutation吵取,不同在于:

  • Action 提交的是 mutation,而不是直接變更狀態(tài)锯厢。
  • Action 可以包含任意異步操作皮官。(vuex調(diào)試器中方法和變化同時(shí)出來(lái))

注冊(cè)action

Action 函數(shù)接受一個(gè)與 store 實(shí)例具有相同方法和屬性的 context 對(duì)象脯倒,因此你可以調(diào)用 context.commit 提交一個(gè) mutation,或者通過(guò) context.statecontext.getters 來(lái)獲取 state 和 getters捺氢。

組件觸發(fā)action

Action 通過(guò) store.dispatch 方法觸發(fā)

接收第二個(gè)參數(shù) payload

eg:

store/index.js 中添加 actions 屬性

  actions:{
    reducePriceA:context=>{
      setTimeout(function(){
        context.commit('reducePriceM')
      },2000)
    }
  }

組建中調(diào)用方法 reducePriceM

<button @click="reducePrice">降價(jià)</button>
<script>
export default {
  methods: {
    reducePrice(){
      this.$store.dispatch('reducePriceA') // actions中的方法
    }
  },
}
</script>

08 Vuex-mapMutations & mapActions

在組件中提交 Mutation

你可以在組件中使用 this.$store.commit('xxx') 提交 mutation藻丢,或者使用 mapMutations 輔助函數(shù)將組件中的 methods 映射為 store.commit 調(diào)用(需要在根節(jié)點(diǎn)注入 store)。

import { mapMutations } from 'vuex'

export default {
  // ...
  methods: {
    ...mapMutations([
      'increment', // 將 `this.increment()` 映射為 `this.$store.commit('increment')`

      // `mapMutations` 也支持載荷:
      'incrementBy' // 將 `this.incrementBy(amount)` 映射為 `this.$store.commit('incrementBy', amount)`
    ]),
    ...mapMutations({
      add: 'increment' // 將 `this.add()` 映射為 `this.$store.commit('increment')`
    })
  }
}

在組件中分發(fā) Action

你在組件中使用 this.$store.dispatch('xxx') 分發(fā) action摄乒,或者使用 mapActions 輔助函數(shù)將組件的 methods 映射為 store.dispatch 調(diào)用(需要先在根節(jié)點(diǎn)注入 store):

import { mapActions } from 'vuex'

export default {
  // ...
  methods: {
    ...mapActions([
      'increment', // 將 `this.increment()` 映射為 `this.$store.dispatch('increment')`

      // `mapActions` 也支持載荷:
      'incrementBy' // 將 `this.incrementBy(amount)` 映射為 `this.$store.dispatch('incrementBy', amount)`
    ]),
    ...mapActions({
      add: 'increment' // 將 `this.add()` 映射為 `this.$store.dispatch('increment')`
    })
  }
}

Module

09 Vuex-Module

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

為了解決以上問(wèn)題,Vuex 允許我們將 store 分割成模塊(module)挤茄。每個(gè)模塊擁有自己的 state如叼、mutation、action穷劈、getter笼恰、甚至是嵌套子模塊——從上至下進(jìn)行同樣方式的分割:

const moduleA = {
  state: { ... },
  mutations: { ... },
  actions: { ... },
  getters: { ... }
}

const moduleB = {
  state: { ... },
  mutations: { ... },
  actions: { ... }
}

const store = new Vuex.Store({
  modules: {
    a: moduleA,
    b: moduleB
  }
})

store.state.a // -> moduleA 的狀態(tài)
store.state.b // -> moduleB 的狀態(tài)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市歇终,隨后出現(xiàn)的幾起案子社证,更是在濱河造成了極大的恐慌,老刑警劉巖评凝,帶你破解...
    沈念sama閱讀 218,386評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件追葡,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡奕短,警方通過(guò)查閱死者的電腦和手機(jī)宜肉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)翎碑,“玉大人谬返,你說(shuō)我怎么就攤上這事∪砧荆” “怎么了遣铝?”我有些...
    開(kāi)封第一講書人閱讀 164,704評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)莉擒。 經(jīng)常有香客問(wèn)我酿炸,道長(zhǎng),這世上最難降的妖魔是什么涨冀? 我笑而不...
    開(kāi)封第一講書人閱讀 58,702評(píng)論 1 294
  • 正文 為了忘掉前任梁沧,我火速辦了婚禮,結(jié)果婚禮上蝇裤,老公的妹妹穿的比我還像新娘廷支。我一直安慰自己,他們只是感情好栓辜,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,716評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布恋拍。 她就那樣靜靜地躺著,像睡著了一般藕甩。 火紅的嫁衣襯著肌膚如雪施敢。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 51,573評(píng)論 1 305
  • 那天狭莱,我揣著相機(jī)與錄音僵娃,去河邊找鬼。 笑死腋妙,一個(gè)胖子當(dāng)著我的面吹牛默怨,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播骤素,決...
    沈念sama閱讀 40,314評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼匙睹,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了济竹?” 一聲冷哼從身側(cè)響起痕檬,我...
    開(kāi)封第一講書人閱讀 39,230評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎送浊,沒(méi)想到半個(gè)月后梦谜,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,680評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡袭景,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,873評(píng)論 3 336
  • 正文 我和宋清朗相戀三年唁桩,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片浴讯。...
    茶點(diǎn)故事閱讀 39,991評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡朵夏,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出榆纽,到底是詐尸還是另有隱情仰猖,我是刑警寧澤,帶...
    沈念sama閱讀 35,706評(píng)論 5 346
  • 正文 年R本政府宣布奈籽,位于F島的核電站饥侵,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏衣屏。R本人自食惡果不足惜躏升,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,329評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望狼忱。 院中可真熱鬧膨疏,春花似錦一睁、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,910評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至饲帅,卻和暖如春复凳,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背灶泵。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,038評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工育八, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人赦邻。 一個(gè)月前我還...
    沈念sama閱讀 48,158評(píng)論 3 370
  • 正文 我出身青樓髓棋,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親深纲。 傳聞我的和親對(duì)象是個(gè)殘疾皇子仲锄,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,941評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容