0

I have made a Vue component that has a v-data-table.

Child Component

    <template>
    <div>
        <v-data-table>
            <template v-slot:item.actions="{ item }">
                <v-tooltip bottom>
                    <template v-slot:activator="{ on, attrs }">
                        <v-icon @click="validation(item)"> mdi-check <v-icon/>
                    <span> Validar movimentação </span>
                </v-tooltip>
            </template>
        </v-data-table>
    </div>
</template>
<script>
export default {
        name: "ViewDraftTable",
        data() {
            return {
                movimentacoesSalvasHeader: [...]
            }
        },
        methods: {
            validation(item) {
                this.$parent.$parent.$parent.$parent.$parent.$parent.$parent.validateMov(item)
            },
        }
    }
</script>

And in the parent, I have the validate() method. How am I supposed to call this method, without having to navigate inside the nested $parents?

2 Answers 2

1

One solution is to use provide/inject, where the parent provides the method, and a deep nested child could inject it where needed:

// Parent.vue
export default {
  provide() {
    return {
      validate(item) {
        /* perform validation */
      }
    }
  }
}

// ViewDraftTable.vue
export default {
  inject: {
    validate: {
      default: item => { /* perform default validation */ }
    }
  },
  methods: {
    validacao(item) {
      this.validate(item)
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You should take a look at a state management library like Vuex

Or use provide/inject

1 Comment

Thx, I know about Vuex but I guess that this is not the case for use it, because it's a simple method call from the child. I'm thinking about something like just reference the method from parent in the child.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.