zoukankan      html  css  js  c++  java
  • Vuex笔记

    npm install vuex --save

    ....../vuex/store.js:

    import Vuex from 'vuex';

    import Vue from 'vue';

    Vue.use(vuex);

    const state = { count:1}

    const mutations = {

        add(state){

        state.count++;   

        },   

       reduce(state){

        state.count--;   

        }

    }

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

    Count.vue

    <template>

    <div>

    <h2>{{msg}}<h2>

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

    <p>

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

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

    </p>

    </div>

    </template>

    <script>

    import store from '@/vuex/store.js'

    export default{

    data(){

       return {

           msg:"hello vuex"  

        }

    },

    store

    }

    </script>

    //可以写成下面这样,然后在上面直接用{{count}}

    computed:{

       count(){

        return this.$store.state.count;

    }

    }

    //还可以这样,在Count.vue中,import { mapState } from 'vuex';  注意一定要有花括号

    computed:mapState({

         count:state=>state.count;

    })

    //还可以这样,在Count.vue中,import {mapState} from 'vuex';

    computed:mapState(['state'])

    MapState:

    add(state,param){

         state.count+=param; 

    }

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

    //还可以这样

    import { mapState,mapMutations } from 'vuex'

    methods:mapMutations(['add','reduce'])

    ...@click='reduce'...

    Getters:

    需要使用扩展运算符

    Actions:

    actions区别于mutations是,actions里的函数是可以异步执行的。

    1.

    2.

    3.

    Modules:

    1.

    2.

  • 相关阅读:
    打印java 对象信息的小技巧
    git 忽略已经跟踪文件的改动
    mysql主从备份方案
    Lucene4.3和Lucene3.5性能对比(二)
    Lucene4.3和Lucene3.5性能对比(一)
    Cracking the coding interview--Q1.1
    CRACKING THE CODING INTERVIEW 笔记(1)
    关于名称重整(name mangling)、多态性的一些简单介绍
    shell中sed用法
    GDB调试GCC(jRate)
  • 原文地址:https://www.cnblogs.com/eret9616/p/8026824.html
Copyright © 2011-2022 走看看