this.$store.state.全局数据名称
import{ mapState } from ‘vuex’
computed:{
...mapState(['count'])
}
完整代码:
<template>
<div>
<h3>当前最新的count值为:{{count}}</h3>
<button>-1</button>
</div>
</template>
<script>
import {mapState} from 'vuex'
export default {
name: 'Subtraction',
data(){
return {
}
},
computed:{
...mapState(['count'])//不用写计算属性的逻辑
}
}
</script>
<style scoped>
</style>
推荐使用mutation变更store中的数据
定义mutation
export default new Vuex.Store({
state: {
count:0
},
mutations: {
add(state){
//变更状态
state.count++
}
},
actions: {
},
modules: {
}
})
触发mutation
methods:{
handlel(){
//触发mutations的第一种方式
this.$store.commit('add')
}
}
完整代码:
store.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
count:0
},
mutations: {
add(state){
state.count++
}
},
actions: {
},
modules: {
}
})
app.vue
<template>
<div id="app">
<my-addition> </my-addition>
<hr/>
<my-subtraction></my-subtraction>
</div>
</template>
<script>
import Addtion from './components/Addtion.vue'
import Subtraction from './components/Subtraction.vue'
export default {
name:'App',
data(){
return{
}
},
components:{
'my-addition':Addtion,
'my-subtraction':Subtraction
}
}
</script>
<style>
</style>
addtion.vue
<template>
<div>
<h3>当前最新的count值为:{{$store.state.count}}</h3>
<button @click="addcount">+1</button>
</div>
</template>
<script>
export default {
name: 'Addtion',
data(){
return{
}
},
methods:{
addcount(){
//第一种方法
this.$store.commit('add')
}
}
}
</script>
<style scoped>
</style>
定义mutation
export default new Vuex.Store({
state: {
count:0
},
mutations: {
addN(state,step){
//变更状态
state.count+=step
}
},
actions: {
},
modules: {
}
})
触发mutation
methods:{
handlel(){
//触发mutations的第一种方式
this.$store.commit('add',N)
}
}
完整案例
store.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
count:0
},
mutations: {
add(state,N){
state.count+=N
}
},
actions: {
},
modules: {
}
})
app.vue
<template>
<div id="app">
<my-addition> </my-addition>
<hr/>
<my-subtraction></my-subtraction>
</div>
</template>
<script>
import Addtion from './components/Addtion.vue'
import Subtraction from './components/Subtraction.vue'
export default {
name:'App',
data(){
return{
}
},
components:{
'my-addition':Addtion,
'my-subtraction':Subtraction
}
}
</script>
<style>
</style>
addtion.vue
<template>
<div>
<h3>当前最新的count值为:{{$store.state.count}}</h3>
<button @click="addcount(10)">+1</button>
</div>
</template>
<script>
export default {
name: 'Addtion',
data(){
return{
}
},
methods:{
addcount(N){
//第一种方法
this.$store.commit('addN',N)
}
}
}
</script>
<style scoped>
</style>
import { mapMutations} from ‘vuex’
methods:{
...mapMutations(['add','addN'])
}
完整代码
store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
count:0
},
mutations: {
add(state){
state.count++
},
addN(state,N){
state.count+=N
}
},
actions: {
},
modules: {
}
})
app.vue
<template>
<div id="app">
<my-addition> </my-addition>
<hr/>
<my-subtraction></my-subtraction>
</div>
</template>
<script>
import Addtion from './components/Addtion.vue'
import Subtraction from './components/Subtraction.vue'
export default {
name:'App',
data(){
return{
}
},
components:{
'my-addition':Addtion,
'my-subtraction':Subtraction
}
}
</script>
<style>
</style>
addtition.vue
<template>
<div>
<h3>当前最新的count值为:{{$store.state.count}}</h3>
<button @click="add()">+1</button>
<button @click="addN(10)">+10</button>
<button @click="myaddN(10)">+10</button>
</div>
</template>
<script>
import {mapMutations} from 'vuex'
export default {
name: 'Addtion',
data(){
return{
}
},
methods:{
...mapMutations(['add','addN']),
myaddN(N){
this.addN(N)
}
}
}
</script>
<style scoped>
</style>
必须使用action来处理异步任务,但是,在action中最终还是要通过触发mutation的方式间接变更数据
export default new Vuex.Store({
state: {
count:0
},
mutations: {
add(state){
state.count++
},
addN(state,N){
state.count+=N
}
},
actions: {
addAsync(context){
setTimeout(()=>{
context.commit('add')
},2000)
},
addAsyncN(context,N){
setTimeout(()=>{
context.commit('addN',N)
},2000)
}
},
modules: {
}
})
<template>
<div>
<h3>当前最新的count值为:{{$store.state.count}}</h3>
<button @click="myaddAsync">+1异步</button>
<button @click="myaddNAsync(100)">+100异步</button>
</div>
</template>
<script>
import {mapMutations} from 'vuex'
export default {
name: 'Addtion',
data(){
return{
}
},
methods:{
myaddAsync(){
//触发actions的第一种方式
this.$store.dispatch('addAsync')
},
myaddNAsync(N){
//触发actions的第一种方式
this.$store.dispatch('addAsyncN',N)
}
}
}
</script>
1. 从vuex中按需导入mapActions函数
import { mapActions} from ‘vuex’
2. 通过刚才导入的mapAction函数,将需要的actions函数,映射为当前组件的methods方法:
触发actions方法
methods:{
...mapActions(['addAsync','addAsyncN']),
}
上例基础上修改addtion.vue文件
addtion.vue
<template>
<div>
<h3>当前最新的count值为:{{$store.state.count}}</h3>
<button @click="myaddAsync">+1异步</button>
<button @click="myaddNAsync(100)">+100异步</button>
<button @click="secaddNAsync(1000)">+1000异步</button>
</div>
</template>
<script>
import {mapMutations,mapActions} from 'vuex'
export default {
name: 'Addtion',
data(){
return{
}
},
methods:{
myaddAsync(){
//触发actions的第一种方式
this.$store.dispatch('addAsync')
},
myaddNAsync(N){
//触发actions的第一种方式
this.$store.dispatch('addAsyncN',N)
console.log(N)
},
...mapActions(['addAsync','addAsyncN']),
secaddNAsync(N){
this.addAsyncN(N)
}
}
}
</script>
<style scoped>
</style>
getter用于对store中的数据进行加工处理成新的数据
定义getter
getters:{
showNum:state=>{
return '当前最新的数量是'+state.count
}
}
使用getters的第一种方法
this.$store.getters.名称
使用getters的第二种方法
import {mapGetters} from 'vuex'
computed:{
...mapGetters(['addcount']),
}
完整代码:
store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
count:0
},
getters:{
addcount:state => {
return state.count+=15
}
},
mutations: {
add(state){
state.count++
},
addN(state,N){
state.count+=N
}
},
actions: {
addAsync(context){
setTimeout(()=>{
context.commit('add')
},2000)
},
addAsyncN(context,N){
setTimeout(()=>{
context.commit('addN',N)
},2000)
}
},
modules: {
}
})
addtion.vue
<template>
<div>
<h3>当前最新的count值为:{{$store.getters.addcount}}</h3>
<h3>当前最新的count值为:{{addcount}}</h3>
<button @click="myaddAsync">+1异步</button>
<button @click="myaddNAsync(100)">+100异步</button>
<button @click="secaddNAsync(1000)">+1000异步</button>
</div>
</template>
<script>
import {mapMutations,mapActions,mapGetters} from 'vuex'
export default {
name: 'Addtion',
data(){
return{
}
},
computed:{
...mapGetters(['addcount']),
},
methods:{
myaddAsync(){
//触发actions的第一种方式
this.$store.dispatch('addAsync')
},
myaddNAsync(N){
//触发actions的第一种方式
this.$store.dispatch('addAsyncN',N)
console.log(N)
},
...mapActions(['addAsync','addAsyncN']),
secaddNAsync(N){
this.addAsyncN(N)
}
}
}
</script>
<style scoped>
</style>