目录
  • 7.action–异步处理
  • 8.module

本教程为入门教程,如有错误,请各位前端大佬指出。

1.什么是vuex

 vuex 是一个专为 vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.详细介绍可以参照官网文档

下面简单介绍vuex

2.安装和引入

先安装vuex。

npm install vuex --save

在main.js中引入后即可使用。

// the vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import vue from 'vue'
import app from './app'
import router from './router'
//vuex使用
import vuex from 'vuex'

vue.use(vuex)
const store = new vuex.store({
    state: {
        //全局变量
        count: 31231
    }
})

vue.config.productiontip = false
    /* eslint-disable no-new */
new vue({
    el: '#app',
    router,
    //vuex必须加入
    store,
    components: { app },
    template: '<app/>'
})

3.vuex的使用

<template>
<div>
      老大有{{showdata}}
      <helloworld2/>
</div>
</template>

<script>
import helloworld2 from './helloworld2'
import son from './son'

export default {
name: 'helloworld',
data () {
  return {
       message2:"",
       cou
    }
},
components:{
  helloworld2,
  son
},computed: {
  showdata(){
    return this.$store.state.count;
  }
}
}

</script>
<template>
<div>
老二有{{$store.state.count}}
</div>
</template>

<script>
export default {
name: 'helloworld2',
data() {
      return {
      }
    }
}
</script>

4.流程介绍

如图当没有使用vuex时流程为: view->actions->state->view

使用了vuex后流程为vuecomponent->(dispatch)actions->(commit)mutations->(mutate)state->(render)->vuecomponent

5.mutation

状态更改,更改 vuex 的 store 中的状态的唯一方法是提交mutation。vuex 中的 mutation 非常类似于事件:每个 mutation 都有一个字符串的事件类型 (type)和一个回调函数 (handler)。这个回调函数就是我们实际进行状态更改的地方,并且它会接受 state 作为第一个参数。

// the vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import vue from 'vue'
import app from './app'
import router from './router'
//vuex使用
import vuex from 'vuex'

vue.use(vuex)
const store = new vuex.store({
    state: {
        //全局变量
        count: 31231
    },
    //更改状态方法
    mutations: {
        //state为上面的state
        adddata(state) {
            // 变更状态
            state.count++
        }
    }
})

vue.config.productiontip = false
    /* eslint-disable no-new */
new vue({
    el: '#app',
    router,
    //vuex必须加入
    store,
    components: { app },
    template: '<app/>'
})

然后执行更改

<template>
<div>
      老大有{{showdata}}
      <helloworld2/>
      <button type = "button" v-on:click = "changedata">  修改按钮    </button>
</div>
</template>

<script>
import helloworld2 from './helloworld2'
import son from './son'

export default {
name: 'helloworld',
data () {
  return {
       message2:"",
    }
},
components:{
  helloworld2,
  son
},computed: {
  showdata(){
    return this.$store.state.count;
  }
},
methods: {
  //执行更改
  changedata(event){
      this.$store.commit("adddata");
  }
}
}

</script>

6.getters过滤

可以限制mutation 比如小于0就不能减少了

// the vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import vue from 'vue'
import app from './app'
import router from './router'
//vuex使用
import vuex from 'vuex'

vue.use(vuex)
const store = new vuex.store({
    state: {
        //全局变量
        count: 0
    },
    //更改状态方法
    mutations: {
        //state为上面的state
        adddata(state) {
            // 变更状态
            state.count++
        }
    },
    //过滤
    getters: {
        getstate(state) {
            if (state.count >= 5) {
                return 5
            } else {
                return state.count
            }
        }
    }
})

vue.config.productiontip = false
    /* eslint-disable no-new */
new vue({
    el: '#app',
    router,
    //vuex必须加入
    store,
    components: { app },
    template: '<app/>'
})

调用时

<template>
<div>
老二有{{$store.getters.getstate}}
</div>
</template>

<script>
export default {
name: 'helloworld2',
data() {
      return {
      }
    }
}
</script>

7.action–异步处理

action 类似于 mutation,不同在于:

action 提交的是 mutation,而不是直接变更状态。 action 可以包含任意异步操作。 mutation只能同步处理
main.js。示例如下:

// the vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import vue from 'vue'
import app from './app'
import router from './router'
//vuex使用
import vuex from 'vuex'

vue.use(vuex)
const store = new vuex.store({
    state: {
        //全局变量
        count: 0
    },
    //更改状态方法
    mutations: {
        //state为上面的state
        adddata(state) {
            // 变更状态
            state.count++
        }
    },
    //过滤
    getters: {
        getstate(state) {
            if (state.count >= 5) {
                return 5
            } else {
                return state.count
            }
        }
    },
    actions: {
        //action触发的mutations方法 优势是异步处理
        adddata(context) {
            //模拟异步
            settimeout(() => {
                context.commit('adddata')
            }, 1000)
        }
    }
})

vue.config.productiontip = false
    /* eslint-disable no-new */
new vue({
    el: '#app',
    router,
    //vuex必须加入
    store,
    components: { app },
    template: '<app/>'
})

在发送时 应该调用action。

<template>
<div>
      老大有{{showdata}}
      <helloworld2/>
      <button type = "button" v-on:click = "changedata">  修改按钮    </button>
</div>
</template>

<script>
import helloworld2 from './helloworld2'
import son from './son'

export default {
name: 'helloworld',
data () {
  return {
       message2:"",
    }
},
components:{
  helloworld2,
  son
},computed: {
  showdata(){
    return this.$store.getters.getstate;
  }
},
methods: {
  //执行更改
  changedata(event){
      //操作mutations方法
      //this.$store.commit("adddata");
      //应该操作action而不是action触发的mutations方法
      this.$store.dispatch("adddata");

  }
}
}

</script>

8.module

由于使用单一状态树,应用的所有状态会集中到一个比较大的对象。当应用变得非常复杂时,store 对象就有可能变得相当臃肿。

为了解决以上问题,vuex 允许我们将 store 分割成模块(module)。每个模块拥有自己的 state、mutation、action、getter、甚至是嵌套子模块——从上至下进行同样方式的分割:

如路由可以分割文件 不在main.js中放入vuex 新建store/index.js

//vuex使用
import vue from 'vue'
import vuex from 'vuex'

vue.use(vuex)

export default new vuex.store({
    state: {
        //全局变量
        count: 0
    },
    //更改状态方法
    mutations: {
        //state为上面的state
        adddata(state) {
            // 变更状态
            state.count++
        }
    },
    //过滤
    getters: {
        getstate(state) {
            if (state.count >= 5) {
                return 5
            } else {
                return state.count
            }
        }
    },
    actions: {
        //action触发的mutations方法 优势是异步处理
        adddata(context) {
            //模拟异步
            settimeout(() => {
                context.commit('adddata')
            }, 1000)
        }
    }
})

修改main.js

// the vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import vue from 'vue'
import app from './app'
import router from './router'
import store from './store'


vue.config.productiontip = false
    /* eslint-disable no-new */
new vue({
    el: '#app',
    router,
    //vuex必须加入
    store,
    components: { app },
    template: '<app/>'
})

我们还能把main.js中的state拿出 新建store/state.js

export default {
    count: 0
}

然后index.js可以改成

//vuex使用
import vue from 'vue'
import vuex from 'vuex'
import state from './state'

vue.use(vuex)

export default new vuex.store({
    state: state,
    //更改状态方法
    mutations: {
        //state为上面的state
        adddata(state) {
            // 变更状态
            state.count++
        }
    },
    //过滤
    getters: {
        getstate(state) {
            if (state.count >= 5) {
                return 5
            } else {
                return state.count
            }
        }
    },
    actions: {
        //action触发的mutations方法 优势是异步处理
        adddata(context) {
            //模拟异步
            settimeout(() => {
                context.commit('adddata')
            }, 1000)
        }
    }
})

总结

到此这篇关于vue入门之vuex安装与使用的文章就介绍到这了,更多相关vuex安装与使用内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!