Javascript 如何在vuejs2中的ajax调用后重新绑定/刷新Dom(如angularjs中的$scope.$apply)?
我正在使用vue.js 2和网页包模板。我是vue.js的新手。 在ajax调用中接收到数据后,我想绑定DOM。 在我的代码中,这里包含v-for,其中每次从api调用接收数据后,响应html都需要用v-html=“tHtml[index]”绑定 当我们在angularjs中使用$scope.$apply()时,可以使用什么来重新绑定或刷新视图/DOM home.vueJavascript 如何在vuejs2中的ajax调用后重新绑定/刷新Dom(如angularjs中的$scope.$apply)?,javascript,ajax,webpack,vue.js,vuejs2,Javascript,Ajax,Webpack,Vue.js,Vuejs2,我正在使用vue.js 2和网页包模板。我是vue.js的新手。 在ajax调用中接收到数据后,我想绑定DOM。 在我的代码中,这里包含v-for,其中每次从api调用接收数据后,响应html都需要用v-html=“tHtml[index]”绑定 当我们在angularjs中使用$scope.$apply()时,可以使用什么来重新绑定或刷新视图/DOM home.vue {{msg}} 从“../service”导入appService 从“../service/bus”导入总线 导出默
{{msg}}
从“../service”导入appService
从“../service/bus”导入总线
导出默认值{
创建(){
appService.checkAuth();
日志(“服务appService val:+appService.user.authenticated”);
//在创建vue后调用
总线。$on('eventBusWithTopic',(数据)=>{//fetch事件总线on ready to get data
//警报(“获取事件!”+数据。用户id);
log(“获取事件!”+数据.user\u id);
this.searchedTopic=data.topic;
this.user\u id=data.user\u id;
总线。$off('eventBusWithTopic');//使用后取消绑定事件
//查看详细信息
})
},
数据(){
返回{
搜索主题:“”,
用户id:“”,
tdata:{},
tHtml:[]
}
},
方法:{
getdata(){
console.log(“dfd”+此用户id);
appService.getdata(this,this.user\u id,this.searchedTopic).success((res)=>{
//log(“res:+JSON.stringify(res));
this.tdata=res.data;
如果(this.tdata.length>0){
//**获取HTML**//console.log(“获取HTML时出错:+JSON.stringify(e)))
}
}
}
您标记为“vuejs2”,但就绪
函数仅在vuejs 1中可用。我认为您必须将代码从就绪
函数添加到创建的
函数。
哪里是$index
?@AmreshVenugopal索引在v-for循环中。您的意思是v-for=“obj in-tdata”
?@AmreshVenugopal是吗
<template>
<div class="row" id="home">
<h3 v-if="msg"><span class="label label-warning">{{msg}}</span></h3>
</div>
<div v-for="obj in tdata">
<div v-html="tHtml[$index]"></div>
</div>
</div>
</template>
<script>
import appService from '../service'
import bus from '../service/bus'
export default {
created() {
appService.checkAuth();
console.log("service appService val: "+appService.user.authenticated);
//called after vue has been created
bus.$on('eventBusWithTopic', (data) => { //fetch event bus on ready to get data
//alert("event fetched! "+data.user_id);
console.log("event fetched! "+data.user_id);
this.searchedTopic = data.topic;
this.user_id = data.user_id;
bus.$off('eventBusWithTopic'); //un-bind event after use
//check details
})
},
data() {
return {
searchedTopic: '',
user_id: '',
tdata: {},
tHtml: []
}
},
methods: {
getdata(){
console.log("dfd "+this.user_id);
appService.getdata(this, this.user_id, this.searchedTopic).success((res) => {
//console.log(" res: "+JSON.stringify(res));
this.tdata = res.data;
if(this.tdata.length > 0){
//**GET HTML** // <<--==--==--==
for(var i=0;i<this.tdata.length;i++){
this.getTemplate(this.tdata[i].id_str);
}
}
if(res.success == 0)
this.msg = res.message;
}).error((e) => console.log("error while getting data: "+JSON.stringify(e)))
},
getTemplate(id){
this.$http.get("https://uqkcgyy8wb.execute-api..*****../user/"+id+"/getHtml", (resp) => {
//return resp.data.html;
this.tHtml[this.tHtml.length] = resp.data.html;
console.log(this.tHtml);
}).error((e) => console.log("error while getting html: "+JSON.stringify(e)))
}
}
}
</script>