2

How to mapping the JSON Object with the variable? I am not sure that my coding is correct or not. I just begin study in Vuejs. Please see my coding I want to map jSON data to the 'country' variable.

  var appZone = new Vue({
	el: '#el',
	data() {
		return {
		  country:  [],
		  shoppingItems: [
			{name: 'apple', price: '10'},
			{name: 'orange', price: '12'}
		  ]
		}
	},
	mounted() {
		axios.get('/wp-json/tour-api/v1/search/11361')
		.then(function (response) {
		  console.log(response);
      this.country = response.json();
		 
		})
		.catch(function (error) {
		  console.log(error);
		});
	  }
  })
<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<div id="el">
<ul>
        <li v-for="item in country">
          {{ item.country_id }}  -  {{ item.title }}
        </li>
</ul>
</div>

Here is my JSON

Here is my JSON data

1 Answer 1

3

Change the mounted function to

mounted() {
    var self = this
    axios.get('/wp-json/tour-api/v1/search/11361')
    .then(function (response) {
      console.log(response);
      self.country = response.data;
    })
    .catch(function (error) {
      console.log(error);
    });
  }

self is being used to maintain a reference to the original this even as the context is changing. It's a technique often used in event handlers (especially in closures).

Sign up to request clarification or add additional context in comments.

2 Comments

did you mean 'var self = this' ?
It should be response.data instead of response.json()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.