Home > Blockchain >  vuejs 3 composition api getCurrentInstance($refs)
vuejs 3 composition api getCurrentInstance($refs)

Time:12-30

I have a problem

I can access $ref in onmounted function but note : getCurrentInstance imported I am getting the following error in submit function

Uncaught TypeError: Cannot read properties of null (reading 'ctx')

const submit = () => {
  getCurrentInstance().ctx.$refs.modalLoading.openModal()
}
onMounted(() => {
  getCurrentInstance().ctx.$refs.modalLoading.closeModal()
})

CodePudding user response:

As getCurrentInstance should normally not been used, you should follow the Template Refs guide.

<!—- inside the template —->
<my-modal ref="modalLoading" />
// your component
export default defineComponent({
  setup() {
    const modalLoading = ref(null);

    const handleSubmit = () => {
      If(!modalLoading.value) return;
      modalLoading.value.openModal();
    }

    return { modalLoading };

  }
})

  • Related