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

178
Views
Vue.js 2.0 - Pasar argumentos en métodos AJAX Axios

Necesito pasar argumentos en métodos usando ajax axios.

 var app = new Vue({ el: '#app', data: { urlAdmission: admissions: [ { name : 'asdf'}, { name : 'sd'} ] }, mounted: function(){ this.allAdmissions() }, methods: { allAdmissions: _.debounce( function(){ var app = this axios.get('http://localhost/school/api/hello') .then( function(response ){ app.admissions = response.data.admissions }) .catch( function(error){ console.log(error) }) }) } });

Como puede ver en montado, llamo a los métodos this.allAdmissions(). Necesito pasar un argumento para poder reutilizar la función. Por ejemplo, this.allAdmissions(' http://localhost/school/api/hello '). Luego utilícelo en axios.get('url'). Gracias

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Parece que lo que está tratando de hacer es crear una función que pueda aceptar una URL y vincular los resultados de la URL a un valor variable en sus datos. Así es como podrías hacerlo.

 methods: { allAdmissions: _.debounce(function(url, value){ axios.get(url) .then(function(response){ this[value] = response.data.admissions }.bind(this)) .catch(function(error){ console.log(error) }) }) }

Entonces, si llamas a ese método así,

 this.allAdmissions('http://localhost/school/api/admissions‌​', "admissions")

allAdmissions establecerá la propiedad de admissions en sus datos al resultado de su llamada. Esto funciona si siempre desea usar response.data.admissions porque lo codificó. Si quisiera que eso también fuera variable, podría pasar un tercer valor así

 methods: { getSomeData: _.debounce(function(url, value, responseValue){ axios.get(url) .then(function(response){ this[value] = response.data[responseValue] }.bind(this)) .catch(function(error){ console.log(error) }) }) }
over 4 years ago · Santiago Trujillo Report

0

En caso de que algunos necesiten múltiples solicitudes de ajax. Aquí hay un ejemplo.

 var app = new Vue({ el: '#app', data: { value: '', admissions: [], schoolyear: [] }, created: function(){ this.ajaxAll() }, methods: { ajaxAll: _.debounce( function(){ var app = this var admissions = 'admissions' var schoolYear = 'schoolyear' axios.all([this.getAllData('http://localhost/school/api/admissions', 'admissions'), this.getAllData('http://localhost/school/api/schoolyear', 'schoolyear')]); }), getAllData: function(url, value){ var app = this return axios.get(url) .then(function(response){ app[value] = response.data[value] console.log(response.data.admissions) }) } } })

Crédito a @Bert Evans.

over 4 years ago · Santiago Trujillo 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!