1
    <template>
            <RecyclablesPopup ref="LVP" class="inline-block m-5px"></RecyclablesPopup>
    </template>

    <script setup>
    import RecyclablesPopup from "../components/popups/RecyclablesPopup";
    import { ref } from 'vue';
    const LVP = ref(null);

    // ... after mounted I have an event with a legacy component and onclick handler:
    eventClick: function(calEvent)
        {
            console.log(LVP.value);
            LVP.value.click();
        }
    </script>

At the end I get Uncaught TypeError: LVP.value.click is not a function after I clicked.

console.log returns me the proxy object as expected Proxy { <target>: Proxy, <handler>: {…} }

Why can't I call click()?

3 Answers 3

3

the click function should be exposed by the child component in order be accessed by the parent component :

RecyclablesPopup component

<script setup>

function click(){
  //.......
}
defineExpose({
 click
})
</script>

for more details please check https://vuejs.org/guide/essentials/template-refs.html#ref-on-component

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. defineExpose did the trick! I thought I could call the normal html element click function, but somehow that does not work.
3

If you are using script setup you can't access functions, variables, etc., defined inside the referenced component. To change that you have to use the defineExpose compiler macro inside RecyclablesPopup component - check more in documentation

//inside RecyclablesPopup
<script setup>
import { ref } from 'vue'

const click = () => {
    //do something
}

defineExpose({
    click,
})
</script>

Comments

0

You need to execute the function on the onMounted lifecycle to guarantee that the component is mounted to the DOM, as the value before the component is mounted will be undefined.

onMounted(() => {
  eventClick()
})

For more resources

https://vuejs.org/api/composition-api-lifecycle.html#onmounted

1 Comment

Same problem with onMounted(() => { LVP.value.click(); });

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.