zoukankan      html  css  js  c++  java
  • vuex的使用

    import Vue from 'vue'
    import Vuex from 'vuex'
    import axios from 'axios'
    
    Vue.use(Vuex);
    
    const state = {
        count:10 //用来定义数据存数据    此处的数据可以在actions里面去接收一下
    }
    const mutations = { //处理数据动作 默认接收一个state 主要处理数据的变化  处理actions的数据
        go(state){
            state.count++
        },
        ww(state){
            state.count--
        },
        xx(state){
            state.count++
        },
        cc(state){
            state.count--
        }
    }
    const getters = {    //在前端组件里只能在computed里接收数据
        count(state){
            return state.b  
        },
        getOdd(state){
            return state.count%2===0?'偶数':'奇数'
        }
    }
    const actions = { //提交数据动作   接收methods里的方法
    go: ({commit}) => { console.log(commit) commit('go'); }, ww({commit}){ commit('ww'); }, xx({commit,state}){ if(commit.count%2===0){ commit('xx') } }, cc({commit}){ //这里异步可以实现ajax请求 new Promise((resolve)=>{ setTimeout(function(){ commit('cc') },1000); }) } } export default new Vuex.Store({ actions, mutations, getters, state })
  • 相关阅读:
    ajax异步更新的理解
    Java 中的匿名内部类
    Java 中的方法内部类
    Java 中的静态内部类
    Java 中的成员内部类
    Java 中的 static 使用之静态方法(转)
    构造方法
    成员变量与局部变量的区别
    script标签属性sync和defer
    jsonp原理
  • 原文地址:https://www.cnblogs.com/l8l8/p/9180446.html
Copyright © 2011-2022 走看看