Vue $在beforeDestroy中发出

时间:2017-07-11 14:55:29

标签: vuejs2 vue-component eventemitter

Vue - 我想跟踪页面上的用户更改,并在他们离开时发送这些更新。基本理念是

//child
beforeDestroy: function() {
  var that = this;
  axios.post('gate/cart.php', userUpdates)
    .then(function(res) {
      if (res.data.success) {
        that.$emit('updateCart', res.data.cart);

//parent (App.vue)   
<router-view
  @updateCart="updateCart"

...
methods: {
  updateCart: function(newCart) {
    alert('caught');
    this.cart = newCart;

dev工具显示发出了emit并发送了正确的有效负载(res.data.cart),但未调用父方法。 (该警报不会触发。)我知道父级中的updateCart方法正在工作,因为另一个组件使用常规方法使用它很好:

addToCart: function() {
  var that = this;
  axios.post('gate/cart.php', this.dataToSend)
    .then(function(res) {
      if(res.data.success === true) {
        that.$emit('updateCart', res.data.cart)            
        that.$router.push({ path: '/product/' + that.product.id})
      }

如果ajax正在工作,我得到一个正确的$ emit,并且目标方法没问题,生命周期钩子警告阻止我执行父方法?你知道更好的方法吗? (我想在更新父数据之前检查ajax调用是否成功。)

0 个答案:

没有答案
相关问题