Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
Vue3: cree la URL de la API y obtenga datos después de cambiar la ruta

Estoy tratando de mostrar las fronteras de un país desde la API de restcountries ( https://restcountries.eu/ ) como botones en los que se puede hacer clic.

así es como trato de construir la url para la API

 mounted() { axios .get(this.urlDetail) .then(response => ( this.details = response.data[0] )) this.borders = this.details.borders.join(";"); this.urlBorders = "https://restcountries.eu/rest/v2/alpha?codes=" + this.borders; fetch(this.urlBorders) .then(res => res.json()) .then(data => this.bordersData = data)

el problema es que la matriz de detalles está vacía en ese momento. Cuando vuelvo a cargar la página, los datos se obtienen correctamente.

Lo intenté:

  • usar beforeMount()
  • use un booleano isFetching
  • obtener los datos con @click-function
  • probado es con estas funciones en mount():
 document.onreadystatechange = () => { if (document.readyState == "complete") { console.log('Page completed with image and files!') } }

estos son mis datos:

 data() { return { isFetching: true, urlDetail: 'https://restcountries.eu/rest/v2/name/' + this.$route.params.countryName, details: [], borders: "", urlBorders: "", bordersData: [] }

este es el html relevante cortado para mostrar los botones:

 <p><strong>Border Countries:</strong><button class="border-button" v-for="border in bordersData"><router-link :to="{ name: 'border', params: {borderName: border.name}}">{{ border.name }}</router-link></button></p>

¡gracias por ayudar!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Trate de esperar las respuestas:

 methods: { async getBorders() { await axios .get(this.urlDetail) .then((response) => (this.details = response.data[0])); }, setBorders() { this.borders = this.details.borders.join(";"); this.urlBorders = "https://restcountries.eu/rest/v2/alpha?codes=" + this.borders; }, async getDets() { await axios .get(this.urlBorders) .then((response) => (this.bordersData = response.data)); }, }, }, async mounted() { await this.getBorders(); this.setBorders(); await this.getDets(); },
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!