Skip to content

Instantly share code, notes, and snippets.

@danielplawgo
Last active December 16, 2020 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielplawgo/9875ecd54e76d7ae0b7ee59bdb457159 to your computer and use it in GitHub Desktop.
Save danielplawgo/9875ecd54e76d7ae0b7ee59bdb457159 to your computer and use it in GitHub Desktop.
Blazor Lazy Loading
@using System.Reflection
<Router AppAssembly="@typeof(Program).Assembly"
AdditionalAssemblies="@assemblies">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
@code {
private List<Assembly> assemblies = new List<Assembly>()
{
typeof(Users.UsersModule).Assembly
};
}
@using System.Reflection
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.WebAssembly.Services
@inject LazyAssemblyLoader assemblyLoader
<Router AppAssembly="@typeof(Program).Assembly"
AdditionalAssemblies="@assemblies"
OnNavigateAsync="@OnNavigateAsync">
<Navigating>
<div style="padding:20px;background-color:blue;color:white">
<p>Loading the requested page&hellip;</p>
</div>
</Navigating>
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
@code {
private List<Assembly> assemblies = new List<Assembly>();
private async Task OnNavigateAsync(NavigationContext args)
{
try
{
if (args.Path.StartsWith("users"))
{
var loadedAssemblies = await assemblyLoader.LoadAssembliesAsync(
new List<string>() { "BlazorLazyLoading.Client.Users.dll" });
assemblies.AddRange(loadedAssemblies);
}
}
catch (Exception ex)
{
}
}
}
<ItemGroup>
<BlazorWebAssemblyLazyLoad Include="BlazorLazyLoading.Client.Users.dll" />
</ItemGroup>
<li class="nav-item px-3">
<NavLink class="nav-link" href="users">
<span class="oi oi-folder" aria-hidden="true"></span> Users
</NavLink>
</li>
@page "/users"
<h3>Users</h3>
<p>This component display the users list.</p>
@code {
}
public class UsersModule
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment