1

I use jQuery ajax to request api and get the respone, but the data cannot be rendered. Does someone know how to fix this problem?

   var app = new Vue({
     //router,
     data: {
      people: null
     },
     created: function() {
      $.ajax({
        url: 'test.php',			
      }).done(function(res){
        console.log(res);
        this.people = JSON.parse(res);
        //console.log('124234234523');
        //console.log(this.people.name);
      });
     }
    }).$mount('#app');
    <div id="app">
      <ol>
        <li v-for="person in people">
          {{ person.name }}
        </li>
      </ol>
    </div>

      
 

Code use vue router: While I use Vue router, even I use arrow function. The template cannot be render by Vue. Whether do I misuse Vue router?

// Listing people component
var PeopleListing = Vue.extend({
  template: '#people-listing-template',
  data: function() {
      return {
        people: this.$parent.people
    }
  }
});


// Create the router
var router = new VueRouter({
	mode: 'hash',
	base: window.location.href,
	routes: [
      {path: '#/', component: PeopleListing},
      {name: 'person', path: '/:id', component: PersonDetail }
    ]
});




var app = new Vue({
    router,
	data: {
		people: null
	},
	created: function() {
		
		$.ajax({
			url: 'test.php',			
		}).done((res) =>{
			//console.log(res);
			this.people = JSON.parse(res);
			//console.log('124234234523');
			//console.log(this.people.name);
		});
	}
}).$mount('#app');
<div id="app">
	<router-view class="view"></router-view>
</div>
  
  <template id="people-listing-template">
    <ul>
		
      <li v-for="person in people">
        {{ person.name }} 
        <router-link :to="{ name: 'person', params: { id: person.guid }}">View Details</router-link>
      </li>
    </ul>
  </template>

1 Answer 1

3

Scope of this is creating issue, use arrow function like following:

var app = new Vue({
 //router,
 data: {
  people: null
 },
 created: function() {
  $.ajax({
    url: 'test.php',            
  }).done((res) => {
    console.log(res);
    this.people = JSON.parse(res);
    //console.log('124234234523');
    //console.log(this.people.name);
  });
 }
}).$mount('#app');

You can get more details about this issue here.

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

2 Comments

Much appreciate for your help. But it doesn't work, if I use <template> and Vue router. Have you try this?
@user39291 Can you add the code with template and vue router.

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.