【发布时间】:2021-07-29 04:06:57
【问题描述】:
我正在从 Mounted Lifecycle Hook 调用一个方法,该方法实际上是调用一个返回响应的后端服务。但是当我将该响应分配给数据时,它不会重新渲染 DOM。我认为它没有得到被动更新。以下是我的代码:
vmObj = new Vue({
el: el[0],
template: notificationTemplate,
mixins: [translation_mixin],
components: {
notification_row
},
data: function() {
return {
'displayedNotification': []
};
},
methods: {
getNotificationData() {
window.restEntity(
'notifications',
'read_notification_pref',
{
'entity': entityData,
'entity_id': entityId
},
this.onSuccess,
true
);
//BackEnd Service which when returns a response we call "onSuccess(resp)"
},
onSuccess(response) {
this.displayedNotification = response.data; //I also used this.$set but nothing happening
}
},
mounted() {
this.getNotificationData();
}
});
更新:
所以真的不是一个答案,但我必须在渲染 Vue 之前调用该服务(之前:
vmObj = new Vue({.....)
例如,使用asyncawait,并将响应保存在一个变量中,然后在Mounted Hook中分配数据,现在它可以工作了。
【问题讨论】:
-
请在
getNotificationData或密码箱中分享代码。见minimal reproducible example -
致电
onSuccess后,您是否在调试器或Vue devtools 中验证displayedNotification具有您期望的正确数据? -
@FrankPl 是的,数据已正确分配给 displayNotification 。但是有些 DOM 是如何不重新渲染的。我注意到一件奇怪的事情,如果我将调试器放在mounted() { this.getNotificationData(); ----->>>>} 在这一行重新渲染正在发生
-
@TJ getNotificationData 是我们拥有的一个默认函数,它执行 ajax 调用,成功时是一个带有响应的回调