Skip to content

Instantly share code, notes, and snippets.

@Mark24Code
Created March 16, 2017 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mark24Code/7ef07a47507da5b1403df0735414e6aa to your computer and use it in GitHub Desktop.
Save Mark24Code/7ef07a47507da5b1403df0735414e6aa to your computer and use it in GitHub Desktop.
使用vuex
<template>
<div class="hello">
<div><p>Hello Components</p></div>
<div>
{{count}}
</div>
<div>
<button @click="increment">Inc</button>
<button @click="decrement">Dec</button>
</div>
</div>
</template>
<script>
import {mapGetters} from 'vuex'
export default {
name: 'hello',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
},
computed:mapGetters([
'count'
]),
methods:{
increment(){
this.$store.dispatch("inc")
},
decrement(){
this.$store.dispatch("dec")
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const state = {
count:500
}
const mutations = {
INC(state){
state.count++
},
DEC(state){
state.count--
}
}
const actions = {
inc({commit}){
commit('INC')
},
dec({commit}){
commit('DEC')
}
}
const getters = {
count(state){
return state.count
}
}
export default new Vuex.Store({
state,
mutations,
actions,
getters
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment