Search code examples
vuejs2onsen-ui2

Onsen + VueJS: Call back from child component (using onsNavigatorProps)


Per documentation here

If page A pushes page B, it can send a function as a prop or data that modifies page A’s context. This way, whenever we want to send anything to page A from page B, the latter just needs to call the function and pass some arguments:

// Page A
this.$emit('push-page', {
  extends: pageB,
  onsNavigatorProps: {
    passDataBack(data) {
      this.dataFromPageB = data;
    }
  }
});

I am following this idea. Doing something similar with this.$store.commit

I want to push AddItemPage and get the returned value copied to this.items

//Parent.vue

pushAddItemPage() {
this.$store.commit('navigator/push', {
  extends: AddItemPage,
  data() {
    return {
      toolbarInfo: {
        backLabel: this.$t('Page'),
        title: this.$t('Add Item')
      }
    }
  },
  onsNavigatorProps: {
    passDataBack(data) {
      this.items = data.splice()   //***this*** is undefined here             
    }
  }
})
},

//AddItemPage.vue   
...    
submitChanges()
{
   this.$attrs.passDataBack(this, ['abc', 'xyz']) // passDataBack() is called, no issues.
},    
...

Only problem is this is not available inside callback function.

So i can't do this.items = data.splice()


Solution

  • current context is available with arrow operator.

    Correct version:

    onsNavigatorProps: {
        passDataBack: (data) => {
          this.items = data.splice()                
        }
    }