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

vuex入門最詳細(xì)整理

 更新時間:2020年03月04日 10:33:19   作者:6666  
在本篇文章里小編給大家分享的是關(guān)于vuex入門最詳細(xì)整理的相關(guān)內(nèi)容,需要的朋友們參考下。

如果你在使用 vue.js , 那么我想你可能會對 vue 組件之間的通信感到崩潰 。

我在使用基于 vue.js 2.0 的UI框架 ElementUI 開發(fā)網(wǎng)站的時候 , 就遇到了這種問題 : 一個頁面有很多表單 , 我試圖將表單寫成一個單文件組件 , 但是表單 ( 子組件 ) 里的數(shù)據(jù)和頁面 ( 父組件 ) 按鈕交互的時候 , 它們之間的通訊很麻煩 :

<!--父組件中引入子組件-->
<template>
 <div>
  <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="show = true">點(diǎn)擊</a>
  <t-dialog :show.sync="show"></t-dialog>
 </div>
</template>

<script>
import dialog from './components/dialog.vue'
export default {
 data(){
  return {
   show:false
  }
 },
 components:{
  "t-dialog":dialog
 }
}
</script>


<!--子組件-->
<template>
 <el-dialog :visible.sync="currentShow"></el-dialog>
</template>

<script>
export default {
 props:['show'],
 computed:{
   currentShow:{
     get(){
       return this.show
     },
     set(val){
       this.$emit("update:show",val)
     }
   }
 }
}
</script>

之所以這么麻煩 , 是因?yàn)楦附M件可以通過 props 給子組件傳遞參數(shù) , 但子組件內(nèi)卻不能直接修改父組件傳過來的參數(shù)。

這時候 , 使用 vuex 就可以比較方便的解決這種問題了 :

<!--父組件中引入子組件-->
<template>
 <div>
  <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="$store.state.show = true">點(diǎn)擊</a>
  <t-dialog></t-dialog>
 </div>
</template>

<script>
import dialog from './components/dialog.vue'
export default {
 components:{
  "t-dialog":dialog
 }
}
</script>


<!--子組件-->
<template>
 <el-dialog :visible.sync="$store.state.show"></el-dialog>
</template>

<script>
export default {}
</script>

是不是方便了許多 , 這就是 vuex 最簡單的應(yīng)用 , 不要被網(wǎng)上其他教程嚇到 , vuex 原來可以這么簡單 !

安裝、使用 vuex

首先我們在 vue.js 2.0 開發(fā)環(huán)境中安裝 vuex :

npm install vuex --save

然后 , 在 main.js 中加入 :

import vuex from 'vuex'
Vue.use(vuex);
var store = new vuex.Store({//store對象
  state:{
    show:false
  }
})

再然后 , 在實(shí)例化 Vue對象時加入 store 對象 :

new Vue({
 el: '#app',
 router,
 store,//使用store
 template: '<App/>',
 components: { App }
})

完成到這一步 , 上述例子中的 $store.state.show 就可以使用了。

modules

前面為了方便 , 我們把 store 對象寫在了 main.js 里面 , 但實(shí)際上為了便于日后的維護(hù) , 我們分開寫更好 , 我們在 src 目錄下 , 新建一個 store 文件夾 , 然后在里面新建一個 index.js :

import Vue from 'vue'
import vuex from 'vuex'
Vue.use(vuex);

export default new vuex.Store({
  state:{
    show:false
  }
})

那么相應(yīng)的 , 在 main.js 里的代碼應(yīng)該改成 :

//vuex
import store from './store'

new Vue({
 el: '#app',
 router,
 store,//使用store
 template: '<App/>',
 components: { App }
})

這樣就把 store 分離出去了 , 那么還有一個問題是 : 這里 $store.state.show 無論哪個組件都可以使用 , 那組件多了之后 , 狀態(tài)也多了 , 這么多狀態(tài)都堆在 store 文件夾下的 index.js 不好維護(hù)怎么辦 ?

我們可以使用 vuex 的 modules , 把 store 文件夾下的 index.js 改成 :

import Vue from 'vue'
import vuex from 'vuex'
Vue.use(vuex);

import dialog_store from '../components/dialog_store.js';//引入某個store對象

export default new vuex.Store({
  modules: {
    dialog: dialog_store
  }
})

這里我們引用了一個 dialog_store.js , 在這個 js 文件里我們就可以單獨(dú)寫 dialog 組件的狀態(tài)了 :

export default {
  state:{
    show:false
  }
}

做出這樣的修改之后 , 我們將之前我們使用的 $store.state.show 統(tǒng)統(tǒng)改為 $store.state.dialog.show 即可。

如果還有其他的組件需要使用 vuex , 就新建一個對應(yīng)的狀態(tài)文件 , 然后將他們加入 store 文件夾下的 index.js 文件中的 modules 中。

modules: {
  dialog: dialog_store,
  other: other,//其他組件
}

mutations

前面我們提到的對話框例子 , 我們對vuex 的依賴僅僅只有一個 $store.state.dialog.show 一個狀態(tài) , 但是如果我們要進(jìn)行一個操作 , 需要依賴很多很多個狀態(tài) , 那管理起來又麻煩了 !

mutations 登場 , 問題迎刃而解 :

export default {
  state:{//state
    show:false
  },
  mutations:{
    switch_dialog(state){//這里的state對應(yīng)著上面這個state
      state.show = state.show?false:true;
      //你還可以在這里執(zhí)行其他的操作改變state
    }
  }
}

使用 mutations 后 , 原先我們的父組件可以改為 :

<template>
 <div id="app">
  <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="$store.commit('switch_dialog')">點(diǎn)擊</a>
  <t-dialog></t-dialog>
 </div>
</template>

<script>
import dialog from './components/dialog.vue'
export default {
 components:{
  "t-dialog":dialog
 }
}
</script>

使用 $store.commit('switch_dialog') 來觸發(fā) mutations 中的 switch_dialog 方法。

這里需要注意的是:

1、mutations 中的方法是不分組件的 , 假如你在 dialog_stroe.js 文件中的定義了

switch_dialog 方法 , 在其他文件中的一個 switch_dialog 方法 , 那么

$store.commit('switch_dialog') 會執(zhí)行所有的 switch_dialog 方法。

2、mutations里的操作必須是同步的。

你一定好奇 , 如果在 mutations 里執(zhí)行異步操作會發(fā)生什么事情 , 實(shí)際上并不會發(fā)生什么奇怪的事情 , 只是官方推薦 , 不要在 mutationss 里執(zhí)行異步操作而已。

actions

多個 state 的操作 , 使用 mutations 會來觸發(fā)會比較好維護(hù) , 那么需要執(zhí)行多個 mutations 就需要用 action 了:

export default {
  state:{//state
    show:false
  },
  mutations:{
    switch_dialog(state){//這里的state對應(yīng)著上面這個state
      state.show = state.show?false:true;
      //你還可以在這里執(zhí)行其他的操作改變state
    }
  },
  actions:{
    switch_dialog(context){//這里的context和我們使用的$store擁有相同的對象和方法
      context.commit('switch_dialog');
      //你還可以在這里觸發(fā)其他的mutations方法
    },
  }
}

那么 , 在之前的父組件中 , 我們需要做修改 , 來觸發(fā) action 里的 switch_dialog 方法:

<template>
 <div id="app">
  <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="$store.dispatch('switch_dialog')">點(diǎn)擊</a>
  <t-dialog></t-dialog>
 </div>
</template>

<script>
import dialog from './components/dialog.vue'
export default {
 components:{
  "t-dialog":dialog
 }
}
</script>

使用 $store.dispatch('switch_dialog') 來觸發(fā) action 中的 switch_dialog 方法。

官方推薦 , 將異步操作放在 action 中。

getters

getters 和 vue 中的 computed 類似 , 都是用來計算 state 然后生成新的數(shù)據(jù) ( 狀態(tài) ) 的。

還是前面的例子 , 假如我們需要一個與狀態(tài) show 剛好相反的狀態(tài) , 使用 vue 中的 computed 可以這樣算出來 :

computed(){
  not_show(){
    return !this.$store.state.dialog.show;
  }
}

那么 , 如果很多很多個組件中都需要用到這個與 show 剛好相反的狀態(tài) , 那么我們需要寫很多很多個 not_show , 使用 getters 就可以解決這種問題 :

export default {
  state:{//state
    show:false
  },
  getters:{
    not_show(state){//這里的state對應(yīng)著上面這個state
      return !state.show;
    }
  },
  mutations:{
    switch_dialog(state){//這里的state對應(yīng)著上面這個state
      state.show = state.show?false:true;
      //你還可以在這里執(zhí)行其他的操作改變state
    }
  },
  actions:{
    switch_dialog(context){//這里的context和我們使用的$store擁有相同的對象和方法
      context.commit('switch_dialog');
      //你還可以在這里觸發(fā)其他的mutations方法
    },
  }
}

我們在組件中使用 $store.state.dialog.show 來獲得狀態(tài) show , 類似的 , 我們可以使用 $store.getters.not_show 來獲得狀態(tài) not_show 。

注意 : $store.getters.not_show 的值是不能直接修改的 , 需要對應(yīng)的 state 發(fā)生變化才能修改。

mapState、mapGetters、mapActions

很多時候 , $store.state.dialog.show 、$store.dispatch('switch_dialog') 這種寫法又長又臭 , 很不方便 , 我們沒使用 vuex 的時候 , 獲取一個狀態(tài)只需要 this.show , 執(zhí)行一個方法只需要 this.switch_dialog 就行了 , 使用 vuex 使寫法變復(fù)雜了 ?

使用 mapState、mapGetters、mapActions 就不會這么復(fù)雜了。

以 mapState 為例 :

<template>
 <el-dialog :visible.sync="show"></el-dialog>
</template>

<script>
import {mapState} from 'vuex';
export default {
 computed:{

  //這里的三點(diǎn)叫做 : 擴(kuò)展運(yùn)算符
  ...mapState({
   show:state=>state.dialog.show
  }),
 }
}
</script>

相當(dāng)于 :

<template>
 <el-dialog :visible.sync="show"></el-dialog>
</template>

<script>
import {mapState} from 'vuex';
export default {
 computed:{
  show(){
    return this.$store.state.dialog.show;
  }
 }
}
</script>

mapGetters、mapActions 和 mapState 類似 , mapGetters 一般也寫在 computed 中 , mapActions 一般寫在 methods 中。

弄懂上面這些 , 你可以去看vuex文檔了 , 應(yīng)該能看懂了。

以上就是vuex入門最詳細(xì)整理的詳細(xì)內(nèi)容,更多關(guān)于vuex入門知識點(diǎn)的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • vue中常用方法的用法匯總

    vue中常用方法的用法匯總

    Vue.js?是一個用于構(gòu)建用戶界面的漸進(jìn)式框架,本文主要為大家整理了一些常用的?Vue?方法及其詳細(xì)說明和代碼示例,有需要的小伙伴可以參考一下
    2023-11-11
  • vue2.0 使用element-ui里的upload組件實(shí)現(xiàn)圖片預(yù)覽效果方法

    vue2.0 使用element-ui里的upload組件實(shí)現(xiàn)圖片預(yù)覽效果方法

    今天小編就為大家分享一篇vue2.0 使用element-ui里的upload組件實(shí)現(xiàn)圖片預(yù)覽效果方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • Vue實(shí)現(xiàn)導(dǎo)出excel表格功能

    Vue實(shí)現(xiàn)導(dǎo)出excel表格功能

    這篇文章主要介紹了Vue實(shí)現(xiàn)導(dǎo)出excel表格的功能,在文章末尾給大家提到了vue中excel表格的導(dǎo)入和導(dǎo)出代碼,需要的朋友可以參考下
    2018-03-03
  • vue函數(shù)@click.prevent的使用解析

    vue函數(shù)@click.prevent的使用解析

    這篇文章主要介紹了vue函數(shù)@click.prevent的使用解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue中使用Sortable的示例代碼

    Vue中使用Sortable的示例代碼

    這篇文章主要介紹了Vue中使用Sortable的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • vue實(shí)現(xiàn)旋轉(zhuǎn)木馬動畫

    vue實(shí)現(xiàn)旋轉(zhuǎn)木馬動畫

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)旋轉(zhuǎn)木馬動畫,圖片數(shù)量無限制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Vue實(shí)現(xiàn)訂單支付倒計時功能

    Vue實(shí)現(xiàn)訂單支付倒計時功能

    這篇文章主要給大家介紹了Vue實(shí)現(xiàn)訂單支付倒計時功能,倒計時這要運(yùn)用在創(chuàng)建訂單后15分鐘內(nèi)進(jìn)行支付,否則訂單取消,本文結(jié)合示例代碼給大家詳細(xì)講解,需要的朋友可以參考下
    2023-08-08
  • vue3 + ElementPlus 封裝列表表格組件包含分頁

    vue3 + ElementPlus 封裝列表表格組件包含分頁

    文章介紹了如何在Vue3和ElementPlus中封裝一個包含分頁功能的通用列表表格組件,組件通過props接收表格數(shù)據(jù)、列配置、總條數(shù)、加載狀態(tài)和分頁配置,并通過events處理分頁和刷新事件,此外,還提供了自定義列內(nèi)容和操作按鈕的功能,感興趣的朋友跟隨小編一起看看吧
    2025-02-02
  • 詳解在Vue中有條件地使用CSS類

    詳解在Vue中有條件地使用CSS類

    本篇文章主要介紹了詳解在Vue中有條件地使用CSS類,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • 如何通過Vue自定義指令實(shí)現(xiàn)前端埋點(diǎn)詳析

    如何通過Vue自定義指令實(shí)現(xiàn)前端埋點(diǎn)詳析

    埋點(diǎn)分析是網(wǎng)站分析的一種常用的數(shù)據(jù)采集方法,下面這篇文章主要給大家介紹了關(guān)于如何通過Vue自定義指令實(shí)現(xiàn)前端埋點(diǎn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07

最新評論

桐柏县| 介休市| 平和县| 阳高县| 柯坪县| 大庆市| 开封市| 马尔康县| 阳山县| 灵璧县| 蓝山县| 文安县| 铜梁县| 吉木乃县| 乌兰察布市| 永登县| 东城区| 屏东市| 海安县| 和龙市| 金平| 平安县| 台东市| 平利县| 临高县| 新安县| 当阳市| 柳江县| 泰宁县| 河北区| 汶川县| 乐平市| 汾西县| 师宗县| 凤冈县| 微山县| 固镇县| 方城县| 麻阳| 云安县| 呼伦贝尔市|