After reading the Vue Composition API documentation, it seems I should be utilizing ref(null)
within a sub-component located inside
<template>...</template>
.
Within this sub-component, there are methods such as open()
, and my current approach to accessing it is as follows:
setup() {
const subcomponentRef= ref(null);
subcomponentRef.value.open();
return { subcomponentRef };
}
Despite attempting to handle the possible error by including a condition like
if (subcomponentRef !== null && subcomponentRef.value !== null) { ... }
, I still encounter the error message Object is possibly 'null'
referencing subcomponentRef.value
. Why is this happening?
I also experimented with accessing it using subcomponentRef?.value?.open()
, only to be met with the error
Property 'open' does not exist on type 'never'
.
Additionally, incorporating Non-null assertions, such as confirmation.value!.open()
, results in the same error
Property 'open' does not exist on type 'never'
.
Is there a mistake in my approach? Perhaps instead of using ref(null)
, should I be initializing it with the actual component? However, I am unsure of the correct method to achieve this as it is not outlined in the documentation.