1. 程式人生 > 其它 >修復瀏覽器報錯 Error in mounted hook (Promise/async): “[object Object]“

修復瀏覽器報錯 Error in mounted hook (Promise/async): “[object Object]“

記錄bug,以便後續遇到同樣問題。

錯誤提示是:

產生原因:

是在生命週期鉤子函式錯誤、可能在生命週期之前呼叫了為定義的函式、方法、物件、物件屬性等

async mounted() {
        document.documentElement.scrollTop = 0;
        let data = (await request().data;
        if (data) {
            this.success = true
        }
        window.addEventListener("scroll", this.windowScroll); //監聽頁面滾動

    }

修改為:

async mounted() {
        try {
            document.documentElement.scrollTop = 0;
            let data = (await request().data;
            if (data) {
                this.success = true
            }
            window.addEventListener("scroll", this.windowScroll); //監聽頁面滾動
        } catch (err) {
            console.log(err)
        }
    }

本文借鑑於https://stackoverflow.com/questions/62291307/error-in-mounted-hook-promise-async-when-fetching-data-from-the-server