How to set page title in blazor?

守給你的承諾、 提交于 2020-08-08 05:16:27

问题


As Blazor being a SPA framework, I would like to know is it possible to set a page title for each individual page in Blazor?

I am currently working on Blazor webassembly project and cannot figure out a way how to add a page title since there is only one index.html as it should be in SPA, but would be really useful if it can be achieved to set title for each "page".


回答1:


1) Provide following script in your index.html (or in an included .js file):

<script>
    setTitle = (title) => { document.title = title; };
</script>

2) In each page inject the jsinterop:

@inject IJSRuntime JSRuntime;

3) After each render, set the document title to the value of your choice:

@code
{
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
       await JSRuntime.InvokeVoidAsync("setTitle", "this is the new title"); ;        
    }    

}



来源:https://stackoverflow.com/questions/58863542/how-to-set-page-title-in-blazor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!