zoukankan      html  css  js  c++  java
  • vuex-count

    //counter.vue
    <template>
      <div id="app">
        Clicked: {{ $store.state.count }} times, count is {{ evenOrOdd }}.
        <button @click="increment">+</button>
        <button @click="decrement">-</button>
        <button @click="incrementIfOdd">Increment if odd</button>
        <button @click="incrementAsync">Increment async</button>
      </div>
    </template>
    
    <script>
    import { mapGetters, mapActions } from 'vuex'
    export default {
      computed: mapGetters([
        'evenOrOdd'
      ]),
      methods: mapActions([
        'increment',
        'decrement',
        'incrementIfOdd',
        'incrementAsync'
      ])
    }
    </script>
    
    //app.js
    import 'babel-polyfill'
    import Vue from 'vue'
    import Counter from './Counter.vue'
    import store from './store'
    
    new Vue({
      el: '#app',
      store,
      render: h => h(Counter)
    })
    
    //index.html
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>vuex counter example</title>
        <link rel="stylesheet" href="/global.css">
      </head>
      <body>
        <div id="app"></div>
        <script src="/__build__/shared.js"></script>
        <script src="/__build__/counter.js"></script>
      </body>
    </html>
    
    //store.js
    import Vue from 'vue'
    import Vuex from 'vuex'
    
    Vue.use(Vuex)
    
    // root state object.
    // each Vuex instance is just a single state tree.
    const state = {
      count: 0
    }
    
    // mutations are operations that actually mutates the state.
    // each mutation handler gets the entire state tree as the
    // first argument, followed by additional payload arguments.
    // mutations must be synchronous and can be recorded by plugins
    // for debugging purposes.
    const mutations = {
      increment (state) {
        state.count++
      },
      decrement (state) {
        state.count--
      }
    }
    
    // actions are functions that causes side effects and can involve
    // asynchronous operations.
    const actions = {
      increment: ({ commit }) => commit('increment'),
      decrement: ({ commit }) => commit('decrement'),
      incrementIfOdd ({ commit, state }) {
        if ((state.count + 1) % 2 === 0) {
          commit('increment')
        }
      },
      incrementAsync ({ commit }) {
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            commit('increment')
            resolve()
          }, 1000)
        })
      }
    }
    
    // getters are functions
    const getters = {
      evenOrOdd: state => state.count % 2 === 0 ? 'even' : 'odd'
    }
    
    // A Vuex instance is created by combining the state, mutations, actions,
    // and getters.
    export default new Vuex.Store({
      state,
      getters,
      actions,
      mutations
    })
    
    You can change the world with your heart,even a lot of changes sometimes unless you won't geiv up....
  • 相关阅读:
    JDK10源码阅读--String
    [java大数据面试] 2018年4月百度面试经过+三面算法题:给定一个数组,求和为定值的所有组合.
    python scrapy爬取知乎问题和收藏夹下所有答案的内容和图片
    读取数据库信息并生成表设计文档Word版本
    怀念那时正规的软件开发流程
    eclipse安装java web插件
    webAPI+angularJS文件上传和下载
    .net整理
    怎么向老板解释你工作的价值?
    JS高级代码
  • 原文地址:https://www.cnblogs.com/xiongwei2017/p/6624972.html
Copyright © 2011-2022 走看看