|
1 | 1 | # PreRenderComponent |
2 | 2 |
|
3 | | -Provides a CascadingValue exposing whether the app is in PreRendering or not. |
| 3 | +Provides a DI Service and/or a CascadingValue exposing whether the app is in PreRendering or not. |
4 | 4 |
|
5 | 5 | ## Usage |
6 | 6 |
|
7 | 7 | Install the nuget https://nuget.org/packages/PreRenderComponent |
8 | 8 |
|
9 | | -Add references to Components/_ViewImports.cshtml |
| 9 | +### Add the service (Required) |
| 10 | +Add the service to your startup Configure method |
| 11 | +This component has a dependency on HttpContextAccessor, so also add that. |
10 | 12 |
|
11 | | -``` |
12 | | -@using PreRenderComponent |
13 | | -@addTagHelper *, PreRenderComponent |
| 13 | +``` CSharp |
| 14 | +services.AddHttpContextAccessor(); |
| 15 | +services.AddScoped<IPreRenderFlag,PreRenderFlag>(); |
14 | 16 | ``` |
15 | 17 |
|
| 18 | +Consume the service wherever you need it (unless you want to use the Cascading Value component) |
| 19 | +``` HTML |
| 20 | +@inject PreRenderComponent.IPreRenderFlag PreRenderFlag |
| 21 | +@if (PreRenderFlag.IsPreRendering) |
| 22 | +{ |
| 23 | + <h1>Pre-Rendering</h1> |
| 24 | +} |
| 25 | +``` |
| 26 | +### Cascading Value (Optional) |
16 | 27 | Wrap the Router component in PreRenderCascade in the App.razor file |
17 | 28 |
|
18 | | -``` |
19 | | -<PreRenderCascade> |
20 | | - <Router AppAssembly="typeof(Startup).Assembly" /> |
21 | | -</PreRenderCascade> |
| 29 | +``` HTML |
| 30 | +<PreRenderComponent.PreRenderCascade> |
| 31 | + <Router AppAssembly="typeof(App).Assembly"> |
| 32 | + <Found Context="routeData"> |
| 33 | + <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" /> |
| 34 | + </Found> |
| 35 | + <NotFound> |
| 36 | + <LayoutView Layout="@typeof(MainLayout)"> |
| 37 | + <p>Sorry, there's nothing at this address.</p> |
| 38 | + </LayoutView> |
| 39 | + </NotFound> |
| 40 | + </Router> |
| 41 | +</PreRenderComponent.PreRenderCascade> |
22 | 42 | ``` |
23 | 43 |
|
24 | 44 | Consume the CascadingValue in your own pages/components |
25 | 45 |
|
26 | | -``` |
| 46 | +``` CSharp |
27 | 47 | @if (IsPreRendering) |
28 | 48 | { |
29 | 49 | <button class="btn btn-dark" onclick="@IncrementCount" disabled>Don't Click me</button> |
|
34 | 54 | } |
35 | 55 |
|
36 | 56 |
|
37 | | -@functions { |
| 57 | +@code { |
38 | 58 | [CascadingParameter(Name = "PreRendering")] |
39 | 59 | protected bool IsPreRendering { get; set; } |
40 | 60 | } |
|
0 commit comments