Vuex介绍(状态管理模式)

27 篇文章 0 订阅
18 篇文章 0 订阅

常用调试:
console.log, alert()阻塞的行为, debugger断点调试


-多个视图依赖同一个状态(eg:菜单导航)
-来自不同视图的行为需要变更同一状态

store.js

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
    state: {
        // 要共用的状态参数
        count: 0
    },
    mutations: {
        // 定义改变的方法
        increase () {
            this.state.count ++
        },
    },
    actions: {
        
    }
})

export default new Vuex.Store({
    state: {
        lists:[]
    },
    mutations: {
        addItem(state, value){
            state.lists.push(value)
        }
    }
})
 
import store from '@/store'
export default {
    name:'Add',
    store,
    data() {
        return {
            title:'',
            content:''
        }
    },
    methods: {
        add(){
            store.commit('addItem', {
                title: this.title,
                content: this.content
            })
            this.title = ''
            this.content = ''
            this.$router.push('/home/list')
        }
    }
}
 // 动态监听list的变化(列表页面实时刷新UI)
<script>
import store from '@/store'
export default {
    name: 'List',
    store,
    computed: {
        pageLists() {
            return store.lists
        }
    }
}
</script>

 

 

Info.vue

<script>
import store from '@/store'
export default {
    name: 'Info',
    store,
    methods: {
        add (){
            console.log('add Event from info!')
            store.commit('increase') // 提交increase,修改count的值
        }
    }
}
</script>

About.vue
 

{{msg}}
<script>
import store from '@/store'
export default {
    name:'about',
    store,
    data() {
        return {
            msg: store.state.count
        }
    }
}
</script>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值