Skip to content

Components @ Lazy

Ivan Sanz Carasa edited this page May 4, 2020 · 12 revisions

<Lazy />

Renders a Component (IComponent) from a Lazy Module based on it's Name or TypeFullName (string).

Render by using TypeFullName

In order to render an IComponent by using TypeFullName, the only requirement is that it exists in a known Lazy Module.

<Lazy Name="MyApplication.Hello" />

This snippet renders a Hello Component assuming the type is MyApplication.Hello. It will automatically fetch the manifests, consume them to locate the Component and download the minimum assemblies to make it work 😄

Named Components

It is possible to manually name your Components and use that name later to resolve them.
This is done by adding a simple attribute BlazorLazyLoading.LazyNameAttribute to your Component:

  • Syntax: .razor

    @attribute [LazyName("SayHello")]
    <h1>Hello!</h3>
  • Syntax: .cs

    [LazyName("SayHello")]
    public class Hello : IComponent { ... }

And then render it like the following:

<Lazy Name="SayHello" />

In order to debug if your component name is generated properly, you can check the contents of _lazy.json:

{
    "MyApplication": {
        "Components": [
            {
                "TypeFullName": "MyApplication.Hello",
                "Name": "SayHello"
            }
        ]
    }
}