vue2.0使用axios请求数据,在回调函数里拿到了请求数据,然后赋值给变量进行循环,但是渲染无效。如果我把数据写死则可以渲染成功。
下面是代码
<template>
<div>
<div v-for="item in objects" class="box">
<el-row>
<el-col :span="18">
<div class="boxCon">
<div class="title">{{item.title}}</div>
<div class="text">
<span class="pubtime">222</span>
<span class="reply">{{item.collect_count}}</span>
</div>
</div>
</el-col>
<el-col :span="6">
<div class="grid-content bg-purple-light">
<img src="../../static/img/1_02.jpg" class="pic">
</div>
</el-col>
</el-row>
</div>
</div>
</template>
<script>
export default {
data(){
return{
objects:[]
}
},
methods:{
lookDetail(e){
var target=event.target;//获取当前元素
var dataid=e;//(pl.id的值);//获取所传参数值
/* this.$router.push({name: 'detail'}) */
this.$router.push({ path: 'detail',query:{id:dataid} })
}
},
mounted:function(){
$.ajax({
url: 'http://api.douban.com/v2/movie/top250',
type: 'GET',
data:{"start":'0',"count":'10'},
dataType: 'JSONP',
success: function (res) {
var self=this;
console.log(res.subjects);
self.objects=res.subjects;
}
})
}
}
</script>
个人觉得渲染无效是因为赋值不成功,但是数据又请求回来了,或者是初始化的时候数据还没有请求回来,但是页面已经渲染完毕造成的。请各位大神多多指点,谢谢
你在这里定义self 和直接用this有区别?
把
var self = this
放ajax外面还有你这用的不是jquery的$.ajax方法吗?哪来的axios
最后,ES6的钩子函数了解一下~