这样可以正常渲染:
<div class="container" id="app">
<ol>
<li v-for="(todo,index) in todos">
{{ index }} {{ todo.text }}
</li>
</ol>
</div>
<script>
new Vue({
el: '#app',
data: {
todos: [
{ text: 'Learn JavaScript' },
{ text: 'Learn Vue' },
{ text: 'Build something awesome' }
]
}
});
</script>
下面把数据改为ajax(使用的axios发送ajax请求:https://github.com/mzabriskie...取回,就不能显示在html中了,console.log(response);
是能够看到取回的数据的。
<div class="container" id="app">
<ol>
<li v-for="(todo,index) in todos">
{{ index }} {{ todo.text }}
</li>
</ol>
</div>
<script>
new Vue({
el: '#app',
data: {
todos: []
},
mounted: function () {
this.$nextTick(function () {
axios.get('/articles')
.then(function (response) {
console.log(response.data);
this.todos = response.data;
})
.catch(function (error) {
console.log(error);
});
});
}
});
</script>
console.log(response.data);
是这样的:
...]
下面这样模拟是可以工作的:
<div class="container" id="app">
<ol>
<li v-for="(todo,index) in todos">
{{ index }} {{ todo.text }}
</li>
</ol>
</div>
<script>
new Vue({
el: '#app',
data: {
todos: []
},
mounted: function () {
this.$nextTick(function () {
var arr=[
{ text: 'Learn JavaScript' },
{ text: 'Learn Vue' },
{ text: 'Build something awesome' }
];
this.todos = arr;
});
}
});
</script>
你这里所用的this的环境变量是next()的回调函数 在调用this.nextTick之前保存一下Vue实例的变量吧