使用keep-alive標籤來提高網頁的性能,把一些已經訪問過的組件內容存儲在內存中,不用每次渲染的時候都要發送ajax請求。
<keep-alive>
//顯示當前路由地址對應的內容
<router-view/>
</keep-alive>
但是有的頁面需要在用戶的操作後數據發生變化,所以當使用keep-alive標籤的時候,vue生命週期中會出現一個activated的函數,需要數據發生變化的時候就重新發送一次ajax請求
activated(){
//當選擇的城市與上一的城市不相同的時候,重新發送ajax請求
if (this.lastCity !== this.city) {
this.lastCity = this.city
this.getHomeInfo()
}
}