• Vuex


    一、Vuex简介 

    解决了前端组件传参的问题,针对当前项目所有的变量进行统一管理,可以理解为前端的数据库

    Vuex分为五个部分

      1.State:单一状态树

       2.Getters:状态获取

       3.Mutations:触发同步事件

       4.Actions:提交mutation,可以包含异步操作

       5.Module:将vuex进行分模块

    二、Vuex版本问题及store.js的使用

    1、安装

    安装最新版本: npm install vuex -S

    2、创建store模块,分别维护state/actions/mutations/getters

    index.js
    state.js
    actions.js
    mutations.js
    getters.js 

     3、在store/index.js文件中新建vuex的store实例

    1. import Vue from 'vue'
    2. import Vuex from 'vuex'
    3. import state from './state'
    4. import getters from './getters'
    5. import actions from './actions'
    6. import mutations from './mutations'
    7. Vue.use(Vuex)
    8. const store = new Vuex.Store({
    9. state,
    10. getters,
    11. actions,
    12. mutations
    13. })
    14. export default store

    4、在main.js中导入并使用store实例

    1. import store from './store'
    2. new Vue({
    3. el: '#app',
    4. router,
    5. store, //在main.js中导入store实例
    6. components: {
    7. App
    8. },
    9. template: '',
    10. data: {
    11. //自定义的事件总线对象,用于父子组件的通信
    12. Bus: new Vue()
    13. }
    14. })

    三、Vuex中的异步同步操作

    处理数据的唯一途径,state的改变或赋值只能在这里

    1. export default {
    2. // type(事件类型): 其值为setResturantName
    3. // payload:官方给它还取了一个高大上的名字:载荷,其实就是一个保存要传递参数的容器
    4. setResturantName: (state, payload) => {
    5. state.resturantName = payload.resturantName;
    6. }
    1. 1:mutations中方法的调用方式
    2. 不能直接调用this.$store.mutations.setResturantName('KFC'),必须使用如下方式调用:
    3. this.$store.commit(type,payload);
    4. // 1、把载荷和type分开提交
    5. store.commit('setResturantName',{
    6. resturantName:'KFC'
    7. })
    8. // 2、载荷和type写到一起
    9. store.commit({
    10. type: 'setResturantName',
    11. resturantName: 'KFC'
    12. })

    一定要记住,Mutation 必须是同步函数。为什么呢?异步方法,我们不知道什么时候状态会发生改变,所以也就无法追踪了
    如果我们需要异步操作,Mutations就不能满足我们需求了,这时候我们就需要Actions了
    mutations: {
    someMutation (state) {
    api.callAsyncMethod(() => {
    state.count++
    })
    }
    }
    异步加载:
    Action类似于 mutation,不同在于:
    1.Action提交的是mutation,而不是直接变更状态
    2.Action可以包含任意异步操作
    3.Action的回调函数接收一个 context 上下文参数,注意,这个参数可不一般,它与 store 实例有着相同的方法和属性
    但是他们并不是同一个实例,context 包含:
    1. state、2. rootState、3. getters、4. mutations、5. actions 五个属性
    所以在这里可以使用 context.commit 来提交一个 mutation,或者通过 context.state 和 context.getters 来获取 state 和 getters。

    四、Vuex后台交互

    action.js

    1. let _this = payload._this;
    2. let url=_this.axios.urls.SYSTEM_MENU_TREE;
    3. _this.axios.post(url,{}).then(r=>{
    4. console.log(r)
    5. }).catch(e=>{
    6. });

     vuePage1:

     _this:this
    

    1、router/index.js

    1. import Vue from 'vue'
    2. import Router from 'vue-router'
    3. import HelloWorld from '@/components/HelloWorld'
    4. import AppMain from '@/components/AppMain'
    5. import LeftNav from '@/components/LeftNav'
    6. import TopNav from '@/components/TopNav'
    7. import Login from '@/views/Login'
    8. import Reg from '@/views/Reg'
    9. import Articles from '@/views/sys/Articles'
    10. import VuexPage1 from '@/views/sys/VuexPage1'
    11. import VuexPage2 from '@/views/sys/VuexPage2'
    12. Vue.use(Router)
    13. export default new Router({
    14. routes: [{
    15. path: '/',
    16. name: 'Login',
    17. component: Login
    18. },
    19. {
    20. path: '/Login',
    21. name: 'Login',
    22. component: Login
    23. },
    24. {
    25. path: '/Reg',
    26. name: 'Reg',
    27. component: Reg
    28. },
    29. {
    30. path: '/AppMain',
    31. name: 'AppMain',
    32. component: AppMain,
    33. children: [{
    34. path: '/LeftNav',
    35. name: 'LeftNav',
    36. component: LeftNav
    37. },
    38. {
    39. path: '/TopNav',
    40. name: 'TopNav',
    41. component: TopNav
    42. },
    43. {
    44. path: '/sys/Articles',
    45. name: 'Articles',
    46. component: Articles
    47. },
    48. {
    49. path: '/sys/VuexPage1',
    50. name: 'VuexPage1',
    51. component: VuexPage1
    52. },
    53. {
    54. path: '/sys/VuexPage2',
    55. name: 'VuexPage2',
    56. component: VuexPage2
    57. }
    58. ]
    59. }
    60. ]
    61. })

    2、store/action.js

    1. export default {
    2. setResNameAsync: (context, payload) => {
    3. // 异步修改值 在异步方法中调用了同步方法
    4. // context指的是Vuex的上下文,相当于 this.$store
    5. // 此代码6s后执行
    6. setTimeout(function(){
    7. context.commit("setResName",payload);
    8. },6000);
    9. let _this = payload._this;
    10. let url=_this.axios.urls.SYSTEM_MENU_TREE;
    11. _this.axios.post(url,{}).then(r=>{
    12. console.log(r)
    13. }).catch(e=>{
    14. });
    15. }
    16. }

    3、store/getters.js

    1. export default{
    2. getResName:(state)=>{
    3. return state.resName;
    4. }
    5. }

    4、store/index.js

    1. import Vue from 'vue'
    2. import Vuex from 'vuex'
    3. import state from './state'
    4. import getters from './getters'
    5. import actions from './actions'
    6. import mutations from './mutations'
    7. Vue.use(Vuex)
    8. const store = new Vuex.Store({
    9. state,
    10. getters,
    11. actions,
    12. mutations
    13. })
    14. export default store

    5、store/mutations.js

    1. export default{
    2. setResName:(state,payload)=>{
    3. // state对象就对应了state.js中的对象
    4. // payload截荷 对应的 传递的 json对象参数{name:zs,age:12}
    5. state.resName = payload.resName;
    6. }
    7. }

    6、store/state.js

    1. export default{
    2. resName:'aaa'
    3. }

    7、views/sys/VuexPage1.vue

    1. <script>
    2. export default{
    3. name:'VuexPage1',
    4. data(){
    5. return {
    6. }
    7. },
    8. methods:{
    9. buy(){
    10. console.log(this.$store);
    11. // 通过commit方法会调用mutations.js文件中定义好的方法
    12. this.$store.commit("setResName",{
    13. resName:'KFC'
    14. })
    15. },
    16. buyAsync(){
    17. this.$store.dispatch("setResNameAsync",{
    18. resName:'麦当劳',
    19. _this:this
    20. })
    21. }
    22. }
    23. ,
    24. computed:{
    25. msg(){
    26. // 从vuex的state文件中获取值
    27. // return this.$store.state.resName;//不推荐,不安全
    28. // 通过getter.js文件获取state.js中定义的变量值
    29. return this.$store.getters.getResName;
    30. }
    31. }
    32. }
    33. script>
    34. <style>
    35. style>

    8、views/sys/VuexPage1.vue

    1. <script>
    2. export default{
    3. name:'VuexPage1',
    4. data(){
    5. return {
    6. }
    7. }
    8. ,
    9. computed:{
    10. msg(){
    11. // 从vuex的state文件中获取值
    12. // return this.$store.state.resName;//不推荐,不安全
    13. // 通过getter.js文件获取state.js中定义的变量值
    14. return this.$store.getters.getResName;
    15. }
    16. }
    17. }
    18. script>
    19. <style>
    20. style>

     ​​​​​​​​​​​​​​

     

     

     

  • 相关阅读:
    微信小程序一键获取位置
    一云多芯 | 云轴科技ZStack助力张家口建设信创资源池
    vim相关命令讲解!
    GlusterFS企业分布式存储
    关于f-stack转发框架的几点分析思考
    CSS 一个好玩的卡片“开卡效果”
    MySQL 日志管理
    八大排序(下)
    文心大模型4.0正式发布!来看看这届百度世界有啥亮点
    AutoCAD Electrical 2022—项目特性
  • 原文地址:https://blog.csdn.net/qq_44247968/article/details/126888229