Skip to content

Instantly share code, notes, and snippets.

@danielplawgo

danielplawgo/App.razor

Last active Dec 16, 2020
Embed
What would you like to do?
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