【问题标题】:如何查看来自 vuex 的存储值?
【发布时间】:2017-09-02 08:50:19
【问题描述】:

我同时使用vuexvuejs 2

我是vuex 的新手,我想看看store 的变量变化。

我想在我的vue component 中添加watch 函数

这是我目前所拥有的:

import Vue from 'vue';
import {
  MY_STATE,
} from './../../mutation-types';

export default {
  [MY_STATE](state, token) {
    state.my_state = token;
  },
};

想知道my_state有没有变化

如何在我的 vuejs 组件中观看 store.my_state

【问题讨论】:

    标签: javascript vue.js vuejs2 vuex


    【解决方案1】:

    例如,假设您有一篮水果, 每次您在篮子中添加或取出水果时,您 想要 (1) 显示有关水果数量的信息,您还 (2) 想要以某种花哨的方式收到水果数量的通知...

    fruit-count-component.vue

    <template>
      <!-- We meet our first objective (1) by simply -->
      <!-- binding to the count property. -->
      <p>Fruits: {{ count }}</p>
    </template>
    
    <script>
    import basket from '../resources/fruit-basket'
    
    export default () {
      computed: {
        count () {
          return basket.state.fruits.length
          // Or return basket.getters.fruitsCount
          // (depends on your design decisions).
        }
      },
      watch: {
        count (newCount, oldCount) {
          // Our fancy notification (2).
          console.log(`We have ${newCount} fruits now, yay!`)
        }
      }
    }
    </script>
    

    请注意,watch 对象中的函数名称必须与computed 对象中的函数名称匹配。在上面的示例中,名称是count

    监视属性的新旧值将作为参数传递给监视回调(计数函数)。

    篮子商店可能是这样的:

    fruit-basket.js

    import Vue from 'vue'
    import Vuex from 'vuex'
    
    Vue.use(Vuex)
    
    const basket = new Vuex.Store({
      state: {
        fruits: []
      },
      getters: {
        fruitsCount (state) {
          return state.fruits.length
        }
      }
      // Obviously you would need some mutations and actions,
      // but to make example cleaner I'll skip this part.
    })
    
    export default basket
    

    您可以在以下资源中阅读更多内容:

    【讨论】:

    • 我只是想知道当watch 操作应该分为两个步骤时我应该怎么做:1)首先,检查所需数据是否被缓存,如果它确实返回缓存的数据; 2) 如果缓存失败,我需要一个异步 ajax 操作来获取数据,但这似乎是 action 的工作。希望我的问题有意义,谢谢!
    • 与 micah5 的回答相比,这样做的好处是什么,它只是在组件中设置了一个观察者,关于存储值?它需要维护的代码更少。
    • @Exocentric 当我写下答案时,我的问题并不清楚。没有上下文为什么需要监视属性。可以这样想:“我想观察变量 X,所以我可以做 Y。”可能这就是为什么大多数答案提出如此截然不同的方法的原因。没有人知道意图是什么。这就是我在回答中包含“目标”的原因。如果你有不同的目标,不同的答案可能适合他们。我的示例只是实验的起点。它并不是一个即插即用的解决方案。没有“好处”,因为好处取决于您的情况。
    • @1Cr18Ni9 我认为缓存不属于组件代码。您最终会过度设计一些应该非常简单的东西(获取数据并将其绑定到视图)。缓存已经在浏览器中实现。您可以通过从服务器发送正确的标头来利用它。这里简单解释一下:csswizardry.com/2019/03/cache-control-for-civilians。您还可以查看 ServiceWorkers,它允许网站在没有互联网连接的情况下运行。
    • 为什么一定要看计算值?为什么计算的值不能为这个值创建一个工作表?这毕竟是计算值的重点。
    【解决方案2】:

    很简单:

    watch: {
      '$store.state.drawer': function() {
        console.log(this.$store.state.drawer)
      }
    }
    

    【讨论】:

    • 这该死的景象比这里的任何答案都直截了当......有没有反对这样做的理由......?
    • 太简单了不像js,js一定更复杂。
    • 超级酷。也对这种方法的任何缺点感兴趣。到目前为止,它似乎运作良好。
    • 说真的。这似乎比接受的答案要好得多,后者需要在 watch 和 computed 中重复函数名称。专家能否评论一下为什么或为什么不这样做?
    • 顺便说一下,如果 store 是用模块命名的,就写'$store.state.module.something'
    【解决方案3】:

    您不应该使用组件的观察者来监听状态变化。我建议您使用 getter 函数,然后将它们映射到您的组件中。

    import { mapGetters } from 'vuex'
    
    export default {
      computed: {
        ...mapGetters({
          myState: 'getMyState'
        })
      }
    }
    

    在您的商店中:

    const getters = {
      getMyState: state => state.my_state
    }
    

    您应该能够通过在组件中使用 this.myState 来收听对商店所做的任何更改。

    https://vuex.vuejs.org/en/getters.html#the-mapgetters-helper

    【讨论】:

    • 我不知道如何实现mapGetters。你能给我举个例子吗。这将是一个很大的帮助。我现在只是执行 GONG 回答。 TY
    • @Rbex "mapGetters" 是 'vuex' 库的一部分。你不需要实现它。
    • 这个答案是错误的。他实际上需要观察计算的属性。
    • getter 调用一次只会获取当时的状态。如果您希望该属性反映另一个组件的状态变化,您必须观察它。
    • 为什么“你不应该使用组件的观察者来监听状态变化”?这是您可能没有想到的示例,如果我想从状态中查看令牌,以及何时更改为重定向到另一个页面。所以,在某些情况下你需要这样做。也许你需要更多的经验才能知道。
    【解决方案4】:

    如上所述,直接在商店中观察变化并不是一个好主意

    但在极少数情况下它可能对某人有用,所以我会留下这个答案。对于其他情况,请参阅@gabriel-robert 回答

    您可以通过state.$watch 执行此操作。将此添加到组件中的 created(或您需要执行此操作的地方)方法中

    this.$store.watch(
        function (state) {
            return state.my_state;
        },
        function () {
            //do something on data change
        },
        {
            deep: true //add this if u need to watch object properties change etc.
        }
    );
    

    更多详情:https://vuex.vuejs.org/api/#watch

    【讨论】:

    • 我认为直接观察状态不是一个好主意。我们应该使用吸气剂。 vuex.vuejs.org/en/getters.html#the-mapgetters-helper
    • @GabrielRobert 我认为两者都有一席之地。如果您需要基于响应式更改模板条件,则使用带有 mapState 的计算值等是有意义的。但除此之外,就像组件中的流量控制一样,您需要一个完整的观察。你是对的,你不应该使用普通的组件观察器,但是 state.$watch 是为这些用例设计的
    • 每个人都提到它,但没有人说为什么!我正在尝试构建一个在更改时与数据库自动同步的 vuex 商店。我觉得看店是最无摩擦的方式!你怎么看?还是不是个好主意?
    【解决方案5】:

    我认为提问者希望将 watch 与 Vuex 一起使用。

    this.$store.watch(
          (state)=>{
            return this.$store.getters.your_getter
          },
          (val)=>{
           //something changed do something
    
          },
          {
            deep:true
          }
          );
    

    【讨论】:

    • 这应该叫什么名字?
    • 可以通过this 访问 Vue 实例的地方。比如,created 钩子。基本上,你需要的任何组件
    【解决方案6】:

    这适用于所有无法用 getter 解决问题并且实际上确实需要观察者的人,例如与非 vue 第三方交流(参见 Vue Watchers 了解何时使用观察者)。

    Vue 组件的观察者和计算值都适用于计算值。所以和vuex没什么区别:

    import { mapState } from 'vuex';
    
    export default {
        computed: {
            ...mapState(['somestate']),
            someComputedLocalState() {
                // is triggered whenever the store state changes
                return this.somestate + ' works too';
            }
        },
        watch: {
            somestate(val, oldVal) {
                // is triggered whenever the store state changes
                console.log('do stuff', val, oldVal);
            }
        }
    }
    

    如果只是结合本地和全局状态,mapState's doc 也提供了一个示例:

    computed: {
        ...mapState({
            // to access local state with `this`, a normal function must be used
            countPlusLocalState (state) {
              return state.count + this.localCount
            }
        }
    })
    

    【讨论】:

    • 不错的 hack,但是太乏味了,你不觉得吗?
    • 如果它在文档中,这不是黑客,是吗?但是,这也不是支持 vue/vuex 的理由
    【解决方案7】:

    如果您只是想观察一个状态属性,然后在组件内根据该属性的变化采取相应的行动,请参见下面的示例。

    store.js:

    export const state = () => ({
     isClosed: false
    })
    export const mutations = {
     closeWindow(state, payload) {
      state.isClosed = payload
     }
    }
    

    在这种情况下,我正在创建一个 boolean 状态属性,我将在应用程序的不同位置进行更改,如下所示:

    this.$store.commit('closeWindow', true)
    

    现在,如果我需要在其他组件中查看该状态属性,然后更改本地属性,我将在 mounted 挂钩中编写以下内容:

    mounted() {
     this.$store.watch(
      state => state.isClosed,
      (value) => {
       if (value) { this.localProperty = 'edit' }
      }
     )
    }
    

    首先,我在 state 属性上设置了一个观察者,然后在回调函数中我使用该属性的 value 来更改 localProperty

    希望对你有帮助!

    【讨论】:

      【解决方案8】:

      如果你使用打字稿,那么你可以:

      import { Watch } from "vue-property-decorator";
      
      ..
      
      @Watch("$store.state.something")
      private watchSomething() {
         // use this.$store.state.something for access
         ...
      }

      【讨论】:

      • 为什么这被否决了?仅仅因为解决方案是针对 vue-class-component 并且 TO 要求使用旧的 vue-class 样式?我觉得前者更可取。也许@Zhang Sol 可以在介绍中提到,这是明确针对 vue-class-component 的?
      • 请注意为什么打字稿装饰器比像这样简单的 vue 原生解决方案更可取:stackoverflow.com/a/56461539/3652783
      • @yann_yinn 很好,因为您的示例不适用于打字稿组件
      • @Desprit true 但问题既没有使用也没有提及 TypeScript。但对于 Typscript 用户来说仍然是一个有用的提示。
      【解决方案9】:

      我确实尝试了一切来使它正常工作。

      理论

      我发现出于某种原因,从$store 更改对象不一定会触发.watch 方法。我的解决方法是

      • 商店
        • 创建一个应该将更改传播到组件的复杂数据集
        • state 中创建一个递增计数器作为标志,确实在监视时将更改传播到组件
        • $store.mutators 中创建一个方法来更改复杂数据集并增加计数器标志
      • 组件
        • 注意$store.state 标志的变化。检测到更改时,从 $store.state 复杂数据集更新本地相关的响应式更改
        • 使用我们的 $store.mutators 方法更改 $store.state 的数据集

      实施

      这是这样实现的:

      商店

      let store = Vuex.Store({
        state: {
          counter: 0,
          data: { someKey: 0 }
        },
        mutations: {
          updateSomeKey(state, value) {
            update the state.data.someKey = value;
            state.counter++;
          }
        }
      });
      

      组件

        data: {
          dataFromStoreDataSomeKey: null,
          someLocalValue: 1
        },
        watch: {
          '$store.state.counter': {
              immediate: true,
              handler() {
                 // update locally relevant data
                 this.someLocalValue = this.$store.state.data.someKey;
              }
           }
        },
        methods: {
          updateSomeKeyInStore() { 
             this.$store.commit('updateSomeKey', someLocalValue);
        }
      

      可运行的演示

      这很复杂,但基本上我们在这里观察一个标志的变化,然后更新本地数据以反映存储在 $state 中的对象的重要变化

      Vue.config.devtools = false
      
      const store = new Vuex.Store({
        state: {
          voteCounter: 0,
          // changes to objectData trigger a watch when keys are added,
          // but not when values are modified?
          votes: {
            'people': 0,
            'companies': 0,
            'total': 0,
          },
        },
        mutations: {
          vote(state, position) {
            state.votes[position]++;
            state.voteCounter++;
          }
        },
      });
      
      
      app = new Vue({
        el: '#app',
        store: store,
        data: {
          votesForPeople: null,
          votesForCompanies: null,
          pendingVote: null,
        },
        computed: {
          totalVotes() {
            return this.votesForPeople + this.votesForCompanies
          },
          peoplePercent() {
            if (this.totalVotes > 0) {
              return 100 * this.votesForPeople / this.totalVotes
            } else {
              return 0
            }
          },
          companiesPercent() {
            if (this.totalVotes > 0) {
              return 100 * this.votesForCompanies / this.totalVotes
            } else {
              return 0
            }
          },
        },
        watch: {
          '$store.state.voteCounter': {
              immediate: true,
              handler() {
                // clone relevant data locally
                this.votesForPeople = this.$store.state.votes.people
                this.votesForCompanies = this.$store.state.votes.companies
              }
           }
        },
        methods: {
          vote(event) {
            if (this.pendingVote) {
              this.$store.commit('vote', this.pendingVote)
            }
          }
        }
        
      })
      <script src="https://cdn.jsdelivr.net/npm/vue@2.6.12"></script>
      <script src="https://unpkg.com/vuex@3.5.1/dist/vuex.js"></script>
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css">
      
      
      <div id="app">
         <form @submit.prevent="vote($event)">
            <div class="form-check">
               <input
                 class="form-check-input" 
                 type="radio" 
                 name="vote" 
                 id="voteCorps"
                 value="companies"
                 v-model="pendingVote"
                >
               <label class="form-check-label" for="voteCorps">
               Equal rights for companies
               </label>
            </div>
            <div class="form-check">
               <input
                 class="form-check-input" 
                 type="radio" 
                 name="vote"
                 id="votePeople" 
                 value="people"
                 v-model="pendingVote"
               >
               <label class="form-check-label" for="votePeople">
               Equal rights for people
               </label>
            </div>
            <button
              class="btn btn-primary"
              :disabled="pendingVote==null"
            >Vote</button>
         </form>
         <div
           class="progress mt-2"
           v-if="totalVotes > 0"
          >
            <div class="progress-bar"
              role="progressbar"
              aria-valuemin="0"
              :style="'width: ' + peoplePercent + '%'"
              :aria-aluenow="votesForPeople"
              :aria-valuemax="totalVotes"
            >People</div>
            <div
              class="progress-bar bg-success"
              role="progressbar"
              aria-valuemin="0"
              :style="'width: ' + companiesPercent + '%'"
              :aria-valuenow="votesForCompanies"
              :aria-valuemax="totalVotes"
            >Companies</div>
         </div>
      </div>

      【讨论】:

      • 您遇到了 Vue 的反应性警告,这是有据可查的:一旦观察到,您就不能再向根数据对象添加反应性属性。因此,建议在创建实例之前预先声明所有根级反应属性。
      • 意思是,更改 votes[xxx] 未预先定义 xxx 不会让您对这些值更改做出反应
      • 有关此问题以及如何解决此问题的更多信息:vuejs.org/v2/guide/reactivity.html#For-Objects
      • @DerekPollard 当我尝试通过创建一个反应式组件来按照您引用的文档中的建议管理数据来解决此问题时,我的项目因超出递归循环计数器而崩溃,这对我来说意味着$store.state 的对象已经是反应性的。此外,当我手动验证$store.state 数据已更改例如通过按钮单击 时,更改会反映在组件中,但此更改不会自动发生。上面的解决方案对我有用。我想要更优雅的东西。
      • 这里的答案是在你的突变中使用Vue.set(state.votes, newVotesObject)
      【解决方案10】:

      通过观察和设置值的变化来创建商店变量的本地状态。 这样 form-input v-model 的局部变量更改不会直接改变 store 变量

      data() {
        return {
          localState: null
        };
       },
       computed: {
        ...mapGetters({
          computedGlobalStateVariable: 'state/globalStateVariable'
        })
       },
       watch: {
        computedGlobalStateVariable: 'setLocalState'
       },
       methods: {
        setLocalState(value) {
         this.localState = Object.assign({}, value);
        }
       }
      

      【讨论】:

        【解决方案11】:

        观察商店变化的最佳方式是使用mapGetters,正如Gabriel所说。 但在某些情况下,您无法通过 mapGetters 进行操作,例如您想使用参数从商店获取一些东西:

        getters: {
          getTodoById: (state, getters) => (id) => {
            return state.todos.find(todo => todo.id === id)
          }
        }
        

        在这种情况下,您不能使用mapGetters。您可以尝试这样做:

        computed: {
            todoById() {
                return this.$store.getters.getTodoById(this.id)
            }
        }
        

        但不幸的是todoByIdwill be updated only if this.id is changed

        如果您希望在这种情况下更新组件,请使用 this.$store.watch solution provided by Gong。或者有意识地处理你的组件,当你需要更新todoById时更新this.id

        【讨论】:

        • 谢谢。这正是我的用例,确实无法观察到 getter ...
        • 至少在我的情况下略有不同return this.$store.getters.getTodoById({id: this.id}) 不确定是否是导致它不响应的对象......但它不是响应的。
        【解决方案12】:

        在组件内部,创建一个计算函数

        computed:{
          myState:function(){
            return this.$store.state.my_state; // return the state value in `my_state`
          }
        }
        

        现在可以查看计算出的函数名,比如

        watch:{
          myState:function(newVal,oldVal){
            // this function will trigger when ever the value of `my_state` changes
          }
        }
        

        vuex 状态my_state 中所做的更改将反映在计算函数myState 中并触发监视函数。

        如果my_state 状态有嵌套数据,那么handler 选项将有更多帮助

        watch:{
          myState:{
            handler:function(newVal,oldVal){
              // this function will trigger when ever the value of `my_state` changes
            },
            deep:true
          }
        }
        

        这将监视商店my_state中的所有嵌套值。

        【讨论】:

          【解决方案13】:

          您也可以订阅存储突变:

          store.subscribe((mutation, state) => {
            console.log(mutation.type)
            console.log(mutation.payload)
          })
          

          https://vuex.vuejs.org/api/#subscribe

          【讨论】:

          • 您可以在组件的 beforeMount() 挂钩中触发它,然后使用 if 语句过滤传入的突变。例如 if(mutation.type == "names/SET_NAMES") {... do something }
          • 在我看来这应该是公认的答案,因为它是基于 vuex 的,您可以选择状态变化的特定部分(mutation.type)
          【解决方案14】:

          您可以结合使用 Vuex actionsgetterscomputed propertieswatchers 来监听 Vuex 状态值的变化。

          HTML 代码:

          <div id="app" :style='style'>
            <input v-model='computedColor' type="text" placeholder='Background Color'>
          </div>
          

          JavaScript 代码:

          'use strict'
          
          Vue.use(Vuex)
          
          const { mapGetters, mapActions, Store } = Vuex
          
          new Vue({
              el: '#app',
            store: new Store({
              state: {
                color: 'red'
              },
              getters: {
                color({color}) {
                  return color
                }
              },
              mutations: {
                setColor(state, payload) {
                  state.color = payload
                }
              },
              actions: {
                setColor({commit}, payload) {
                  commit('setColor', payload)
                }
              }
            }),
            methods: {
              ...mapGetters([
                  'color'
              ]),
              ...mapActions([
                  'setColor'
              ])
            },
            computed: {
              computedColor: {
                  set(value) {
                  this.setColor(value)
                },
                get() {
                  return this.color()
                }
              },
              style() {
                  return `background-color: ${this.computedColor};`
              }
            },
            watch: {
              computedColor() {
                  console.log(`Watcher in use @${new Date().getTime()}`)
              }
            }
          })
          

          See JSFiddle demo.

          【讨论】:

            【解决方案15】:

            当你想在状态级别观看时,可以这样做:

            let App = new Vue({
                //...
                store,
                watch: {
                    '$store.state.myState': function (newVal) {
                        console.log(newVal);
                        store.dispatch('handleMyStateChange');
                    }
                },
                //...
            });
            

            【讨论】:

            • 通过dispatch 状态操作从组件处理store.state 更改不是一个好主意,因为此行为仅在您使用该组件时才有效。你也可能以无限循环结束。注意store.state 更改很少使用,例如,如果您有一个组件或页面应该基于store.state 执行某些操作,更改无法通过仅在您无法比较newValueoldValue 的情况下使用计算的mapState 处理
            • @Januartha 那么你对这个问题有什么建议呢?
            • @Andy 当然是它的工作。我只想说明你为什么打电话给store.dispatch?如果你想处理store.state 更改为store' why not handle it inside store.mutations`?
            • @BillalBEGUERADJ I prever dube 解决方案更干净
            • @Januartha,因为在进行突变之前可能会发生 ajax 调用,这就是我首先使用store.dispatch 的原因。例如,每当$store.state.country 更改时,我想从一个国家/地区获取所有城市,所以我将其添加到观察者。然后我会写一个ajax调用:在store.dispatch('fetchCities')我写:axios.get('cities',{params:{country: state.country }}).then(response =&gt; store.commit('receiveCities',response) )
            【解决方案16】:

            Vue 在字符串状态下观察

            状态:

            $store.state.local_store.list_of_data
            

            内部组件

              watch: {
                   
                   '$store.state.local_store.list_of_data':{//<----------your state call in string
                    handler(){
                        console.log("value changeing in party sales entry"); //<---do your stuff here
                    },
                    deep:true
                   }
            
                },
            

            【讨论】:

              【解决方案17】:

              在计算中使用你的 getter,然后观察它并做你需要的事情

                  computed:{
                  ...mapGetters(["yourGetterName"])
               },
               watch: {
                  yourGetterName(value) {
                     // Do something you need
                  },
              
                }
              

              【讨论】:

                【解决方案18】:

                你也可以在你的 vue 组件中使用 mapState 来直接从 store 中获取状态。

                在您的组件中:

                computed: mapState([
                  'my_state'
                ])
                

                其中my_state 是来自商店的变量。

                【讨论】:

                  【解决方案19】:

                  ====== store =====
                  import Vue from 'vue'
                  import Vuex from 'vuex'
                  import axios from 'axios'
                  
                  Vue.use(Vuex)
                  
                  export default new Vuex.Store({
                    state: {
                      showRegisterLoginPage: true,
                      user: null,
                      allitem: null,
                      productShow: null,
                      userCart: null
                    },
                    mutations: {
                      SET_USERS(state, payload) {
                        state.user = payload
                      },
                      HIDE_LOGIN(state) {
                        state.showRegisterLoginPage = false
                      },
                      SHOW_LOGIN(state) {
                        state.showRegisterLoginPage = true
                      },
                      SET_ALLITEM(state, payload) {
                        state.allitem = payload
                      },
                      SET_PRODUCTSHOW(state, payload) {
                        state.productShow = payload
                      },
                      SET_USERCART(state, payload) {
                        state.userCart = payload
                      }
                    },
                    actions: {
                      getUserLogin({ commit }) {
                        axios({
                          method: 'get',
                          url: 'http://localhost:3000/users',
                          headers: {
                            token: localStorage.getItem('token')
                          }
                        })
                          .then(({ data }) => {
                            // console.log(data)
                            commit('SET_USERS', data)
                          })
                          .catch(err => {
                            console.log(err)
                          })
                      },
                      addItem({ dispatch }, payload) {
                        let formData = new FormData()
                        formData.append('name', payload.name)
                        formData.append('file', payload.file)
                        formData.append('category', payload.category)
                        formData.append('price', payload.price)
                        formData.append('stock', payload.stock)
                        formData.append('description', payload.description)
                        axios({
                          method: 'post',
                          url: 'http://localhost:3000/products',
                          data: formData,
                          headers: {
                            token: localStorage.getItem('token')
                          }
                        })
                          .then(({ data }) => {
                            // console.log('data hasbeen created ', data)
                            dispatch('getAllItem')
                          })
                          .catch(err => {
                            console.log(err)
                          })
                      },
                      getAllItem({ commit }) {
                        axios({
                          method: 'get',
                          url: 'http://localhost:3000/products'
                        })
                          .then(({ data }) => {
                            // console.log(data)
                            commit('SET_ALLITEM', data)
                          })
                          .catch(err => {
                            console.log(err)
                          })
                      },
                      addUserCart({ dispatch }, { payload, productId }) {
                        let newCart = {
                          count: payload
                        }
                        // console.log('ini dari store nya', productId)
                  
                        axios({
                          method: 'post',
                          url: `http://localhost:3000/transactions/${productId}`,
                          data: newCart,
                          headers: {
                            token: localStorage.getItem('token')
                          }
                        })
                          .then(({ data }) => {
                            dispatch('getUserCart')
                            // console.log('cart hasbeen added ', data)
                          })
                          .catch(err => {
                            console.log(err)
                          })
                      },
                      getUserCart({ commit }) {
                        axios({
                          method: 'get',
                          url: 'http://localhost:3000/transactions/user',
                          headers: {
                            token: localStorage.getItem('token')
                          }
                        })
                          .then(({ data }) => {
                            // console.log(data)
                            commit('SET_USERCART', data)
                          })
                          .catch(err => {
                            console.log(err)
                          })
                      },
                      cartCheckout({ commit, dispatch }, transactionId) {
                        let count = null
                        axios({
                          method: 'post',
                          url: `http://localhost:3000/transactions/checkout/${transactionId}`,
                          headers: {
                            token: localStorage.getItem('token')
                          },
                          data: {
                            sesuatu: 'sesuatu'
                          }
                        })
                          .then(({ data }) => {
                            count = data.count
                            console.log(count, data)
                  
                            dispatch('getUserCart')
                          })
                          .catch(err => {
                            console.log(err)
                          })
                      },
                      deleteTransactions({ dispatch }, transactionId) {
                        axios({
                          method: 'delete',
                          url: `http://localhost:3000/transactions/${transactionId}`,
                          headers: {
                            token: localStorage.getItem('token')
                          }
                        })
                          .then(({ data }) => {
                            console.log('success delete')
                  
                            dispatch('getUserCart')
                          })
                          .catch(err => {
                            console.log(err)
                          })
                      }
                    },
                    modules: {}
                  })

                  【讨论】:

                  • 欢迎来到本站。只放一个代码 sn-p 是不够的。请提供有关您的代码的一些解释。
                  【解决方案20】:

                  我用过这个方法,效果很好:

                  store.js:

                  const state = {
                    createSuccess: false
                  };
                  

                  mutations.js

                  [mutations.CREATE_SUCCESS](state, payload) {
                      state.createSuccess = payload;
                  }
                  

                  actions.js

                  async [mutations.STORE]({ commit }, payload) {
                    try {
                      let result = await axios.post('/api/admin/users', payload);
                      commit(mutations.CREATE_SUCCESS, user);
                    } catch (err) {
                      console.log(err);
                    }
                  }
                  

                  getters.js

                  isSuccess: state => {
                      return state.createSuccess
                  }
                  

                  在您使用商店状态的组件中:

                  watch: {
                      isSuccess(value) {
                        if (value) {
                          this.$notify({
                            title: "Success",
                            message: "Create user success",
                            type: "success"
                          });
                        }
                      }
                    }
                  

                  当用户提交表单时,STORE动作会被调用,创建成功后,CREATE_SUCCESS突变被提交。将createSuccess设为true,在组件中watcher会看到value发生变化并触发通知。

                  isSuccess 应该与您在 getters.js

                  中声明的名称匹配

                  【讨论】:

                    猜你喜欢
                    • 2021-09-29
                    • 2021-12-03
                    • 1970-01-01
                    • 2016-12-16
                    • 1970-01-01
                    • 1970-01-01
                    • 2021-01-18
                    • 1970-01-01
                    • 2019-06-17
                    相关资源
                    最近更新 更多