1. /**
    2. * 在 vuex 中 获取数据大多使用辅助方法
    3. *
    4. * mapState
    5. * mapGetters
    6. * mapMutations 放在 methods 中 同步更改数据
    7. * mapActions 放在 methods 中 异步步更改数据
    8. *
    9. * */
    10. // 全局辅助方法
    11. import { mapState, mapGetters, mapMutations,mapActions } from 'vuex';
    12. export default {
    13. computed: {
    14. // lesson() {
    15. // return this.$store.state.lesson
    16. // }
    17. // mapState 封装之后
    18. ...mapState(['lesson','time']) ,// 获取 state 中的数据
    19. // lesson 更改名字 u
    20. // ...mapState({
    21. // u:state =>state.lesson
    22. // },'time') // 获取 state 中的数据
    23. // 拿取子模块 user 中的数据
    24. ...mapState('user',['userName']),
    25. // mapGetters
    26. ...mapGetters(['getName']),
    27. ...mapGetters( 'user',['getUserName']),
    28. },
    29. methods: {
    30. ...mapMutations(['changeTime']), // 仅仅处于函数还没执行
    31. // 同步更改子模块 user 中的数据
    32. ...mapMutations('user',['changeUser']),
    33. // 异步更改数据
    34. ...mapActions(['changeTime']),
    35. change () {
    36. // 同步 直接执行 changeTime 函数
    37. // 通过 commit 唤醒 mutations changeTime 提交 mutation 类型
    38. // this.$store.commit('changeTime', '20191029')
    39. // this.$store.commit('changeUser', '白茶清欢')
    40. // this.changeTime('20191029');
    41. // this.changeUser('白茶清欢');
    42. // mapActions 异步步更改数据
    43. // this.$store.dispatch('changeTime','20191212')
    44. this.changeTime('20191212');
    45. this.changeUser('白茶清欢');
    46. },
    47. }
    48. }