Home >Web Front-end >JS Tutorial >Application of state manager in VueX

Application of state manager in VueX

不言
不言Original
2018-08-04 10:05:031524browse

This article introduces you to the application of the state manager in VueX. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

VueX State Manager

cnpm i vuex axios -S
1 创建Vuex 仓库
import Vue from 'vue'
import Vuex from 'vuex'
vue.use(Vuex)
const store = new VueX.store({
    state: {//存放状态},
    mutations:{//唯一修改状态的地方,不在这里做逻辑处理}
})
export default store

2 在入口文件main.js下引入store
import store from './store/index.js'
将store 放到根实例里  以供全局使用
new Vue({
    el:'#app',
    store,
    components:{App},
    template:<App/>
})
开始使用store(以home组件为例)

The use of Vuex is also progressive. You can start with the simplest and use it as your experience and skills increase. For the use of progressive enhancement, if the use of vuex is calculated according to levels, you can start from the most basic t1 level, first summarize the first three most basic versions, and then summarize the others when you have time.

T1 level

1.
在hoome/script.js中进行请求数据
import Vue from 'vue'
import axios from 'axios'
export default {
    mounted(){
        axios.get('请求数据的接口')
        .then((res)=>{this.$store.commit('changeList',res.data)})
           //changeList相当于调用了在store.mutations中定义的修改状态的方法
                    //res.data  就是在改变状态时要修改的数据,需要在这传递过去。
        .catch((err)=>{console,log(err)})
        }
    }
2.
在store/index.js中定义
import Vue from 'vue'
import Vuex from 'vuex'
vue.use(Vuex)
const store = new VueX.store({
    state: {
        //存放状态
        list: [ ]     //存放一个空的数组
},
    mutations:{
        //唯一修改状态的地方,不在这里做逻辑处理
        //定义一个修改list的方法
            //state 指上面存放list的对象,data 为在请求数据出传过来请求到的数据
        changeList (state,data) {
            state.list = data  //将请求来的数据赋值给list
      }
    }
   })
export default store

3.
在home/index.vue中渲染
<template>
    //渲染数据的时候通过this.store.state.list直接从store中取数据
    //还可以从其他组件通过这种方法去用这个数据无需重新获取
    <li v-for=&#39;item of this.store.state.list&#39; :key=&#39;item.id&#39;>{{item.name}}</li>
</template>

Note: If we obtain the data in the home component, it can be used in other components, but when The page refresh will enter the home page by default, which is equivalent to modifying the data, and then clicking on other pages will also have data. If the data we obtain in the user component is to be used in the home component, the data will not be displayed when the page is refreshed, because there is no method to trigger the change of data in the user component at this time, so the data is empty. When the user page is clicked , there will be data. At this time, click on the home page and we will be able to see the data obtained from the user component in the home component. The solution to this problem can be to save the data locally or request the data on the homepage

T2 level

When the page is rendered We need to get the data through this.store.state. This way of writing is too long and not very good.
Use calculated attributes combined with mapState to solve this problem

1
在home/script.js中进行操作
import Vue from 'vue'
import mapState from 'vuex'
import axios from 'axios'
export default {
    computed:{
        //mapState为辅助函数
        ...mapState(['list'])
    },
    mounted(){
        axios.get('请求数据的接口')
        .then((res)=>{this.$store.commit('changeList',res.data)})
        .catch((err)=>{console,log(err)})
        }
    }

2
在home/index.vue中渲染
<template>
    <li v-for=&#39;item of  list&#39; :key=&#39;item.id&#39;>{{item.name}}</li>
</template>

T3 level

Use constants to replace event types (easy to view status and facilitate maintenance)
##

    1
    在store下创建mutation-type.js
    export const  CHANGE_LIST = 'CHANGE_LIST'
    
    2
    在store/index.js引入mutation-type.js
    import Vue from 'vue'
    import Vuex from 'vuex'
    import {CHANGE_LIST }  from‘./mutation-type.js’
    vue.use(Vuex)
    const store = new VueX.store({
        state: {
            list: [ ]     //存放一个空的数组
    },
        mutations:{
        //我们可以使用Es6风格的计算属性命名功能来使用一个常量作为函数名
            [CHANGE_LIST] (state,data) {
                state.list = data  //将请求来的数据赋值给list
          }
        }
       })
    export default store
    
    3
    在home/script.js中进行引入
    import Vue from 'vue'
    import mapState from 'vuex'
    import axios from 'axios'
    import {CHANGE_LIST} from ‘../../store/mutation-type.js’
    export default {
        computed:{
            //mapState为辅助函数
            ...mapState(['list'])
        },
        mounted(){
            axios.get('请求数据的接口')
            .then((res)=>{this.$store.commit(CHANGE_LIST,res.data)})
            .catch((err)=>{console,log(err)})
            }
        }
Recommended related articles:

Permissions in vue Control the creation process of dynamic routing (picture and text)

What is the difference between the vue command and $nextTick to operate DOM?

The above is the detailed content of Application of state manager in VueX. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn