(Vue全家桶)Vue-vuex

vuex入门

vuex是一个专门为vue.js设计的集中式状态管理架构。状态?我把它理解为在data中的属性需要共享给其他vue组件使用的部分,就叫做状态。

1.安装vuex在控制命令行输入

npm install vuex --save

2..新建一个vuex文件夹(这个不是必须的),并在文件夹下新建store.js文件,文件中引入我们的vue和vuex。

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


Vue.use(Vuex);

const state={
    count:1
}

const mutations={
    add(state){
        state.count++;
    },
    reduce(state){
        state.count--;
    }
}
//用export default 封装代码,让外部可以引用
export default new Vuex.Store({
    state,
    mutations
})

实现对vuex中的count进行加减预览。

<template>
    <div>
        <h2>{{msg}}</h2>
        <hr/>
        <H3>{{$store.state.count}}</H3>
<!--在count.vue模板中加入两个按钮,并调用mutations中的方法-->
        <button @click="$store.commit('add')">+</button>
        <button @click="$store.commit('reduce')">-</button>
    </div>
</template>

<script>
import store from '@/vuex/store'

export default {
    name: 'Count',
    data(){
        return{
            msg:'Hello Vue'
        }
    },
    store
}
</script>

state访问状态对象

在文件夹下新建store.js文件,文件中引入我们的vue和vuex。

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

Vue.use(Vuex);

const state={
    count:1,
    status:'开始'
}

通过$方法赋值

  {{$store.state.count}}

通过computed的计算属性直接赋值

直接可以{{count}}-{{status}}获取到state里面的值

computed:{
        count(){
            return this.$store.state.count;
        },
        status(){
            return this.$store.state.status;
        }
    }

通过mapState的对象来赋值

我们首先要用import引入mapState

import {mapState} from 'vuex';

然后还在computed计算属性里写如下代码


    computed:mapState({
        count: state=>state.count,
        status: state=>state.status
    })

通过mapState的数组来赋值

我们首先要用import引入mapState

import {mapState} from 'vuex';

这个算是最简单的写法了,在实际项目开发当中也经常这样使用。

 computed:mapState(["count","status"])

Mutations修改状态

在文件夹下新建store.js文件

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


Vue.use(Vuex);

const state={
    count:1,
    status:'开始'
}

const mutations={
    //n是可以传递过来的参数
    add(state,n){
        state.count+=n;
    },
    reduce(state){
        state.count--;
    }
}

export default new Vuex.Store({
    state,
    mutations
})

$store.commit( )

在Count.vue里修改按钮的commit( )方法传递的参数,我们传递10,意思就是每次加10.

 <button @click="$store.commit('add',10)">+</button>
 <button @click="$store.commit('reduce')">-</button>

模板获取Mutations方法

<template>
    <div>
        <h2>{{msg}}</h2>
        <hr/>
        <H3>{{count}}-{{status}}</H3>
        <button @click="add(10)">+</button>
        <button @click="reduce">-</button>
    </div>
</template>

<script>
import store from '@/vuex/store'
//在模板count.vue里用import 引入我们的mapMutations:
import {mapState,mapMutations} from 'vuex'
export default {
    name: 'Count',
    data(){
        return{
            msg:'Hello Vue'
        }
    },
    computed:mapState(["count","status"]),
    //在模板的<script>标签里添加methods属性,并加入mapMutations
    methods:mapMutations(["add","reduce"]),
    store
}
</script>

getters计算过滤操作

getters从表面是获得的意思,可以把他看作在获取数据之前进行的一种再编辑,相当于对数据的一个过滤和加工。你可以把它看作store.js的计算属性。

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


Vue.use(Vuex);

const state={
    count:1,
    status:'开始'
}

const mutations={
    //n是可以传递过来的参数
    add(state,n){
        state.count+=n;
    },
    reduce(state){
        state.count--;
    }
}
//二次编辑给数字后面加上单位
const getters={
    count:state=>state.count+"个"
}

export default new Vuex.Store({
    state,
    mutations,
    getters
})

getters基本用法

computed:{
        ...mapState(["count","status"]),
        count(){
            return this.$store.getters.count;
        }
    },

用mapGetters简化模板写法

    computed:{
        ...mapState(["count","status"]),
        ...mapGetters(["count"])
    },
    met

actions异步修改状态

actions和之前讲的Mutations功能基本一样,不同点是,actions是异步的改变state状态,而Mutations是同步改变状态。

  • context:上下文对象,这里你可以理解称store本身。
  • {commit}:直接把commit对象传递过来,可以让方法体逻辑和代码更清晰明了。
import Vue from 'vue'
import Vuex from 'vuex'


Vue.use(Vuex);

const state={
    count:1,
    status:'开始'
}

const mutations={
    //n是可以传递过来的参数
    add(state,n){
        state.count+=n;
    },
    reduce(state){
        state.count--;
    }
}

const getters={
    count:state=>state.count+"个"
}

const actions={
    addAction(context){
       context.commit("add",10);
    },
    reduceAction({commit}){
        commit("reduce")
    }
}

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

模板中的使用

<template>
    <div>
        <p>
            <button @click="addAction">+</button>
            <button @click="reduceAction">-</button>
        </p>
    </div>
</template>

<script>
import store from '@/vuex/store'
import {mapState,mapMutations,mapGetters,mapActions} from 'vuex'
export default {
    name: 'Count',
    data(){
        return{
            msg:'Hello Vue'
        }
    },
    methods:{
        ...mapActions(["addAction","reduceAction"])
    }
}
</script>

module模块组

随着项目的复杂性增加,我们共享的状态越来越多,这时候我们就需要把我们状态的各种操作进行一个分组,分组后再进行按组编写。那今天我们就学习一下module:状态管理器的模块组操作。

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


Vue.use(Vuex);

const state={
    count:1,
    status:'开始'
}

const mutations={
    //n是可以传递过来的参数
    add(state,n){
        state.count+=n;
    },
    reduce(state){
        state.count--;
    }
}

const getters={
    count:state=>state.count+"个"
}

const actions={
    addAction(context){
       context.commit("add",10);
    },
    reduceAction({commit}){
        commit("reduce")
    }
}

const modeA={
    state,
    mutations,
    getters,
    actions 
}


export default new Vuex.Store({
    modules:{a:modeA}
})

在模板使用

<h3>{{$store.state.a.count}}</h3>

如果想用简单的方法引入,还是要在我们的计算属性中rutrun我们的状态。写法如下:

computed:{
    count(){
        return this.$store.state.a.count;
    }
},
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容

  • 让阳光照在我的身体上 我的世界没有灰霾 让雨水敲打在我的额头 我的发是湿润而有型的 我没有时间唉叹和报怨 我内心的...
    壹風阅读 354评论 5 7
  • 今天感觉好累好想什么都不做 好想明天可以什么都不想就睡懒觉 可是 不能 而且 那样做我可能也并不会快乐 我想做的有...
    阿楠的小窝阅读 145评论 0 0
  • 我慢慢地、慢慢地了解到,所谓父女母子一场,只不过意味着,你和他的缘分就是今生今世不断地在目送他的背影渐行渐远。你站...
    法蒂玛阅读 244评论 11 8
  • 因为young仔一时兴起,开了两个专题。不知为何,最近这两个专题的投稿汹涌地铺面而来,young仔实在有些招架不住...
    昭阳2017阅读 768评论 16 7