Vuejs不会从ajax渲染数据

时间:2017-03-16 06:18:21

标签: vuejs2

我使用jQuery ajax来请求api并获得respone,但无法呈现数据。有人知道如何解决这个问题吗?



   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>

      
 
&#13;
&#13;
&#13;

代码使用vue路由器: 虽然我使用Vue路由器,但我使用箭头功能。 Vue无法呈现模板。我是否滥用Vue路由器?

&#13;
&#13;
// 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');
&#13;
<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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

这是创建问题的范围,使用如下箭头功能:

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');

您可以获得有关此问题的更多详细信息here