Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

256
Visualizações
Use method from child component in parent with CDN vue.js

I just switchend from alpine.js to vue.js (cdn) because i experiment with async frunctions. My goal is to call a method from the parent which is defined in a component.

But when i want to call "test()" in my html file i get the error that "Cannot read properties of undefined (reading 'childRef')" All works fine when the method is defined at the "app" -element (parent) but i dont want to clutter this element with different methods.

Thanks in advance for every hint or remarks to my code.

Code Snippet Vue/Javascript

--------------------------------------
const app = Vue.createApp ({
  data() {
    return {
      visible: false,
      openTab: ''
    }
  },
  methods: {
    test() {
      this.$resfs.childRef.getMyDevices();
    }
  }
})
--------------------------------------
app.component('child',{
  data() {
    return {
      myDevices: {},
    }
  },
  methods: {
    async getMyDevices() {
      const response = await foo.getMyDevices();
      const data = await {...response.devices};

      this.myDevices = data;
    }
  },
  template: `<div ref="childRef"></div>`
})

Code Snippet HTML

<div @click="test()>
  <child ref="childRef"></child>
</div>
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

There was a typo in your $refs accessing:

methods: {
    test() {
      this.$refs.childRef.getMyDevices(); // $resfs befores
    }
  }
over 4 years ago · Santiago Trujillo Relatório

0

There is a typo in your question, jus correct the refs spelling

this.$refs.childRef.getMyDevices()

If the above doesn't work then try using

this.$children[0].getMyDevices()
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda