Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to call a method from a callback defined in data() in Vue 2.7?

I use a 3rd party component called HelloWorld. The component accepts a prop called closeCallbacks. It is an array of callbacks that are called when the HelloWorld component is closed.

I can not change the way the 3rd party component works.

I implement the template of my component to use HelloWorld component and pass closeCallbacks from the data:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<template>
    <HelloWorld :closeCallbacks="closeCallbacks" />
</template>

Then, I define the array of callbacks, where one callback item contains a property called action required by HelloWorld component. The action callback is called when HelloWorld is closed:

<script lang="ts">
export default defineComponent({
    data() {
        return {
            closeCallbacks: [
                {
                    action: function () {
                        return this.handleCloseCallback();
                    },
                },
            ],

        };
    },
    methods: {
        handleCloseCallback: function () {
        //
        },
    },
});
</script>

When the action callback is called, I want to call a method of my component that is called handleCloseCallback (as pictured above).

However in Vue 2.7 I get an error:

Property 'handleCloseCallback' does not exist on type '{ name: string; action: (item: any) => void; }'.

When I will change handleCloseCallback to an array function:

handleCloseCallback: () => {
            //
            },

I get another error:

Property 'handleCloseCallback' does not exist on type 'CreateComponentPublicInstance<{}, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, {}, {}, {}, false, OptionTypesType<{}, {}, {}, {}, {}, {}>, ... 5 more ..., {}>'.

My question is: how to call a method from a callback defined in data?

>Solution :

Would this fix your issue?

<template>
  <HelloWorld v-if="closeCallbacks.length" :closeCallbacks="closeCallbacks" />
</template>
<script>
export default defineComponent({
  data: () => ({
    closeCallbacks: []
  }),
  mounted() {
    this.closeCallbacks = [{ action: this.handleCloseCallback }]
  },
  methods: {
    handleCloseCallback() {}
  }
})
<script>

Point being: this.handleCloseCallback is defined on mounted, but it’s not yet defined at the time data is parsed.
Note I used a v-if to defer rendering of the components until closeCallbacks is populated, to avoid the initial rendering of the component with an empty closeCallbacks.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading