N
N
nurdus2018-07-11 12:48:57
Vue.js
nurdus, 2018-07-11 12:48:57

Why does a parent method called from a child return nothing to the child?

Good afternoon.
I guess I'm trying to misuse parent and child, BUT I want to ask anyway.
So there is, a descendant:

// IterableTableTemplate.vue (child)
<template>
  <!--//-->
    <tr v-for="item in items" :key="item._id">
      <td v-for="(field, keyField) in fields" :key="keyField">{{ calcSomething(item, keyField) }}</td>
    </tr>
  <!--//-->
</template>
<script>
// ...imports
export default {
  props: {
    items: Array,
    fields: Object
  },
  methods: {
    calcSomething: function(item, field) {
      this.$emit("getSomething", item, field)
    }
    /*, // если код прописать прямо в потомке, то всё работает
    calcSomething: function(job, field) {
      return "111"
    }
    */
}
</script>

and parent
//parent.vue
<template>
  <IterableTableTemplate :items="jobs" :fields="fields" @getSomething="doSomething">
  </IterableTableTemplate>
</template>
<script>
//...imports
export default {
  data() {
    return {
      jobs,
      fields
    }
  },
  methods: {
    doSomething: function(job, field) {
      console.log(job, field) // отрабатывает job и field есть
      return "111"                // НО ничего не возвращает
    }
}
</script>

Question: is this how it should work? And if YES, why doesn't it work like that?
In this case, the method simply converts some data, and it's probably better to do it in the parent, and "feed" everything prepared to the child.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2018-07-11
@nurdus

Where is he supposed to return it?
It should work well, but where will it return?
The result of this function is not used anywhere and cannot be used unless something is done in the function itself.
emit only throws an event, the parent is raised to the event, but this has nothing to do with anything, completely different processes.
And the @getSomething handler is simply executed, and its result goes nowhere.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question