Vue.js監聽頁面加載完成後添加業務邏輯

document.onreadystatechange = () => {
  if (document.readyState == "complete") {
    console.log('Page completed with image and files!')
    // fetch to next page or some code
  }
}

注意如果使用Accordion等元素,並且有多個在Vue循環裏的話,需要等頁面全部加載完成了才能初始化Accordion,不然只有第一個Accordion是生效的,因爲頁面沒加載完全的話循環裏的html還沒有輸出完全,只有模版.vue裏的一個是寫好的,其他都還沒生成,所以需要等頁面全部加載完成了,再去初始化Accordion組件才能全部識別到所有循環的元素,我試了上面的方式第一次可以,但頁面刷新後又沒用了,最後用了下面的updated的方式才最終解決

補充:

 

mounted() {
    window.addEventListener('load', () => {
         // run after everything is in-place
    })
},

// 99% using "mounted()" with the code above is enough, 
// but incase its not, you can also hook into "updated()"
//
// keep in mind that any code in here will re-run on each time the DOM change
updated() {
    // run something after dom has changed by vue
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章