
通过修改 loadDataList 方法实现数据自动刷新
在 Vue 中,数据驱动着视图。这意味着当底层数据发生改变时,视图将随之更新。因此,仅需修改 loadDataList 方法中的数据即可实现自动刷新。
目前,loadDataList 方法只创建了 Vue 实例,而没有修改数据。为了实现自动刷新,需要在方法中修改 app 实例的 searchResultList 数据。
以下是修改后的代码:
立即学习“前端免费学习笔记(深入)”;
function loadDataList() {
var newDataList;
if (document.getElementById('searchIpt').value == '1') {
newDataList = [{'title': 'hello3'}, {'title': 'hello4'}];
} else if (document.getElementById('searchIpt').value == '2') {
newDataList = [{'title': 'hello5'}, {'title': 'hello6'}];
}
app.searchResultList = newDataList;
}在这个修改后的方法中,我们根据搜索输入判断新数据,并更新 app 实例的 searchResultList 数据。这将触发视图的自动更新,从而实现页面数据的自动刷新。










