美文网首页Vue.js程序员Web前端之路
真正掌握vuex的使用方法(六)

真正掌握vuex的使用方法(六)

作者: 张培跃 | 来源:发表于2018-05-27 08:11 被阅读109次

下面咱们来将切换的案例改为vuex来写!
首先需要在src目录下,新建一个store文件夹,然后在该文件夹内创建一个store.js文件

import Vue from 'vue';//引用vue
import Vuex from 'vuex';//引用vuex
Vue.use(Vuex);//使用vuex
const state={
    tagList:[],//用于存放与切换相关的数据
};
const mutations={
    //用于改变state下的tagList状态值
    SET_TAGLIST(state,v){//这里的state即是上面定义的state常量
        state.tagList=v;
    }
}
export default new Vuex.Store({//暴露Store对象
    state,
    mutations,//将mutations进行暴露
})

main.js为:

import Vue from 'vue'
import App from './App'
import router from './router'
import store from './vuex/store'//导入store.js
Vue.config.productionTip = false
new Vue({
    el: '#app',
    router,
    store,//添加store
    components: { App },
    template: '<App/>'
})

app.vue为:

<template>
<div id="app">
    <!--对按钮进行遍历-->
   <input type="button" v-for="(item,i) in tagList" :value="item.tagName" :class="{active:i==index}" @click="index=i">
    <!--对新闻进行遍历-->
    <div v-for="(item,i) in tagList" v-show="i==index">
        <p v-for="info in item.newList"><a :href="info.newHref">{{info.newTitle}}</a></p>
    </div>
</div>
</template>
<script>
    import axios from "axios";
    import {mapState} from "vuex";
    export default {
        name: 'App',
        data(){
            return {
                //index用于记录当前所选按钮的位置,值会根据点击按钮的不同而变化
                index:0
            }
        },
        computed:{
            ...mapState(["tagList"])
        },
        mounted(){
            axios.get("/static/tagList.json")
                .then(data=>{
                    this.$store.commit("SET_TAGLIST",data.data);
                })
        }
    }
</script>
<style>
    #app input,#app p{
        margin:5px;
        padding:5px;
    }
    #app input.active{
        background:red;
    }
    #app div{
        border:1px solid red;
    }
</style>

npm run dev,运行一次,一切正常!
到目前为止,相信大家看以上的代码应该都不会有太大问题了,所以不做解释!
咱们知道,对多个 state 的操作 , 使用 mutations 来操作比较好维护 , 但mutations 只可以写一些同步操作,那异步操作放到哪里呢?比如咱们的axios放在哪里比较合适呢?在这个时候咱们就可以用到action了。通过action来操作mutations最终来改变state的值。
接下来在store.js中添加actions:

import Vue from 'vue';//引用vue
import Vuex from 'vuex';//引用vuex
import axios from "axios"
Vue.use(Vuex);//使用vuex
const state={
    tagList:[]
};
const mutations={
    //用于改变state下的tagList状态值
    SET_TAGLIST(state,v){//这里的state即是上面定义的state常量
        state.tagList=v;
    }
}
const actions={
    getTagList:function(context){//这里的context和我们使用的$store拥有相同的对象和方法
        axios.get("/static/tagList.json")
            .then(data=>{
                context.commit("SET_TAGLIST",data.data);
                //根据需要,咱们还可以在此处触发其它的mutations方法
            })
    }
}
export default new Vuex.Store({//暴露Store对象
    state,
    mutations,//将mutations进行暴露
    actions//将actions进行暴露
})

那么接下来就要在App.vue中来触发action下的方法getTagList:

import {mapState} from "vuex";
export default {
    name: 'App',
    data(){
        return {
            //index用于记录当前所选按钮的位置,值会根据点击按钮的不同而变化
            index:0
        }
    },
    computed:{
        ...mapState(["tagList"])
    },
    mounted(){
        //使用 $store.dispatch('getTagList') 来触发 action 中的 getTagList 方法。
        this.$store.dispatch("getTagList");
    }
}

使用 $store.dispatch('getTagList') 来触发 action 中的 getTagList 方法。也推荐大家在action里来写一些异步方法!
当然调用action的方法也有简写的形式:

//引入mapActions
import {mapState,mapActions} from "vuex";
export default {
    name: 'App',
    data(){
        return {
            //index用于记录当前所选按钮的位置,值会根据点击按钮的不同而变化
            index:0
        }
    },
    methods:{
        //通过mapActions添加上action当中需要的方法getTagList
        ...mapActions(["getTagList"])
    },
    computed:{
        ...mapState(["tagList"])
    },
    mounted(){
        //直接调用 即可
        this.getTagList();
    }
}

npm run dev 运行,依旧完美!
未完,待续!


foot.png

相关文章

  • 真正掌握vuex的使用方法(六)

    下面咱们来将切换的案例改为vuex来写!首先需要在src目录下,新建一个store文件夹,然后在该文件夹内创建一个...

  • 真正掌握vuex的使用方法(四)

    接下来看一下template当中计算总票数的表达式: 是不是很长?是不是看着它很蓝瘦?正常的第一反应就是将其写入到...

  • 真正掌握vuex的使用方法(五)

    希望每一位同学可以亲自将每行代码进行一次尝试!记得对于学习代码来讲慢慢来才会更快! 现在咱们先抛开vuex,一起来...

  • 真正掌握vuex的使用方法(一)

    导语:vuex是什么?我的理解就是vuex是一个管理者,管理的方式是集中式管理,管理的对象即是vue.js应用程序...

  • 真正掌握vuex的使用方法(二)

    从上篇文章当中相信大家已经对vuex有了一些大概了解了,接下来咱们结合实例来继续敲代码吧!切记一定要动手实操练习一...

  • 真正掌握vuex的使用方法(三)

    接下来咱们继续使用vuex来完成上篇文章的投票实例。大家一定要记住,学习编程这种事一定要慢慢来才会快!所以一定要将...

  • 真正掌握vuex的使用方法(七)----完结

    今天是关于vuex的最后一篇文章了!怎么说呢?且行且珍惜吧!!认真尝试每一行代码! 之前的文章当中,我们把所有的数...

  • vuex与axios的优化写法

    vuex与axios的优化写法 封装方法 使用方法 vuex: action.js get post put de...

  • Vuex

    今天在博客项目中用到了vuex,记录下vuex 的使用方法。Vuex 是一个专为 Vue.js 应用程序开发的状态...

  • vuex使用记录

    副标题:vuex使用详解、vue使用全局变量、vue使用 store 这篇博客主要记录了 vuex的使用方法,简单...

网友评论

    本文标题:真正掌握vuex的使用方法(六)

    本文链接:https://www.haomeiwen.com/subject/arurjftx.html