問題:OnInitializedAsync 會跑二次!
於 Blazor Server App 應用程式中, OnInitializedAsync 觸發二次,經測試確實有此問題。
引言
於 Blazor Server App 應用程式中, OnInitializedAsync 觸發二次,經測試確實有此問題。模拜google大神原因出在預設的 render-mode="ServerPrerendered" 此模式下 OnInitializedAsync 會觸發二次,解決方式改設定 render-mode="Server" 就行了。
紀錄
在 _Html.cshtml
變更 App 的 render-mode
為 "Server" 即可。

render-mode 官方文件中英文
Server
2
轉譯 Blazor 伺服器端應用程式的標記。 這不包含元件的任何輸出。 當使用者代理程式啟動時,它會使用此標記來啟動 blazor 應用程式。 Renders a marker for a Blazor server-side application. This doesn't include any output from the component. When the user-agent starts, it uses this marker to bootstrap a blazor application.
ServerPrerendered
3
將元件轉譯為靜態 HTML,並包含 Blazor 伺服器端應用程式的標記。 當使用者代理程式啟動時,它會使用此標記來啟動 blazor 應用程式。 Renders the component into static HTML and includes a marker for a Blazor server-side application. When the user-agent starts, it uses this marker to bootstrap a blazor application.
Static
1
將元件轉譯為靜態 HTML。 Renders the component into static HTML.
WebAssembly
4
轉譯 Blazor Webassembly 應用程式的標記。 這不包含元件的任何輸出。 當使用者代理程式啟動時,它會使用此標記來啟動 blazor 用戶端應用程式。 Renders a marker for a Blazor webassembly application. This doesn't include any output from the component. When the user-agent starts, it uses this marker to bootstrap a blazor client-side application.
WebAssemblyPrerendered
5
將元件轉譯為靜態 HTML,並包含 Blazor Webassembly 應用程式的標記。 當使用者代理程式啟動時,它會使用此標記來啟動 blazor 用戶端應用程式。 Renders the component into static HTML and includes a marker for a Blazor webassembly application. When the user-agent starts, it uses this marker to bootstrap a blazor client-side application.
從文獻推論
在 ServerPrerendered (已證實) 與 WebAssemblyPrerendered (未證實) 兩種模式下,OnInitializedAsync()
、OnParametersSetAsync()
會觸發兩次。
相關文章
Last updated