Vuex起手示例
main.ts
new Vue({
store
}).$mount('#app')
index.ts
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
count:0
} ,
mutations: {
add(state,payload:number){ //此处只能传两个参数,有多个参数时可将其全部封装到payload对象中
state.count += payload
}
}
})
vue组件
读:
对象 computed
@Component{
computed:{
count(){
return this.$store.state.count
}
}
}
类 JS/ts
get count() {
return this.$store.state.count
}
在ts中,若要引用count,只能使用getter语法
写:
this.$store.commit('add',payload:number)