• 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....
  • 相关阅读:
    Python之pytest 基础
    unittest和pytest的区别
    Selenium 常用定位对象元素的方法
    ORCAl存储过程
    Mysql的存储过程
    TestNG 搭建测试框架 自动化测试
    通过junit/TestNG+java 实现自动化测试
    查看APP用到的图片方法
    码农干私活的建议(转)
    Android的onCreateOptionsMenu()创建菜单Menu详解(转)
  • 原文地址:https://www.cnblogs.com/xiongwei2017/p/6624972.html
Copyright © 2020-2023  润新知