You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/blazor/lifecycle.md
+19-1
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ description: Learn how to use Razor component lifecycle methods in ASP.NET Core
5
5
monikerRange: '>= aspnetcore-3.1'
6
6
ms.author: riande
7
7
ms.custom: mvc
8
-
ms.date: 12/18/2019
8
+
ms.date: 03/17/2020
9
9
no-loc: [Blazor, SignalR]
10
10
uid: blazor/lifecycle
11
11
---
@@ -48,6 +48,8 @@ To prevent developer code in `OnInitializedAsync` from running twice, see the [S
48
48
49
49
While a Blazor Server app is prerendering, certain actions, such as calling into JavaScript, aren't possible because a connection with the browser hasn't been established. Components may need to render differently when prerendered. For more information, see the [Detect when the app is prerendering](#detect-when-the-app-is-prerendering) section.
50
50
51
+
If any event handlers are set up, unhook them on disposal. For more information, see the [Component disposal with IDisposable](#component-disposal-with-idisposable) section.
52
+
51
53
### Before parameters are set
52
54
53
55
<xref:Microsoft.AspNetCore.Components.ComponentBase.SetParametersAsync*> sets parameters supplied by the component's parent in the render tree:
@@ -67,6 +69,8 @@ The default implementation of `SetParametersAsync` sets the value of each proper
67
69
68
70
If `base.SetParametersAync` isn't invoked, the custom code can interpret the incoming parameters value in any way required. For example, there's no requirement to assign the incoming parameters to the properties on the class.
69
71
72
+
If any event handlers are set up, unhook them on disposal. For more information, see the [Component disposal with IDisposable](#component-disposal-with-idisposable) section.
73
+
70
74
### After parameters are set
71
75
72
76
<xref:Microsoft.AspNetCore.Components.ComponentBase.OnParametersSetAsync*> and <xref:Microsoft.AspNetCore.Components.ComponentBase.OnParametersSet*> are called:
If any event handlers are set up, unhook them on disposal. For more information, see the [Component disposal with IDisposable](#component-disposal-with-idisposable) section.
101
+
96
102
### After component render
97
103
98
104
<xref:Microsoft.AspNetCore.Components.ComponentBase.OnAfterRenderAsync*> and <xref:Microsoft.AspNetCore.Components.ComponentBase.OnAfterRender*> are called after a component has finished rendering. Element and component references are populated at this point. Use this stage to perform additional initialization steps using the rendered content, such as activating third-party JavaScript libraries that operate on the rendered DOM elements.
`OnAfterRender` and `OnAfterRenderAsync`*aren't called when prerendering on the server.*
131
137
138
+
If any event handlers are set up, unhook them on disposal. For more information, see the [Component disposal with IDisposable](#component-disposal-with-idisposable) section.
139
+
132
140
### Suppress UI refreshing
133
141
134
142
Override <xref:Microsoft.AspNetCore.Components.ComponentBase.ShouldRender*> to suppress UI refreshing. If the implementation returns `true`, the UI is refreshed:
@@ -181,6 +189,16 @@ If a component implements <xref:System.IDisposable>, the [Dispose method](/dotne
181
189
> [!NOTE]
182
190
> Calling <xref:Microsoft.AspNetCore.Components.ComponentBase.StateHasChanged*> in `Dispose` isn't supported. `StateHasChanged` might be invoked as part of tearing down the renderer, so requesting UI updates at that point isn't supported.
183
191
192
+
Unsubscribe event handlers from .NET events. The following [Blazor form](xref:blazor/forms-validation) examples show how to unhook an event handler in the `Dispose` method:
Copy file name to clipboardExpand all lines: aspnetcore/blazor/routing.md
+39-8
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ description: Learn how to route requests in apps and about the NavLink component
5
5
monikerRange: '>= aspnetcore-3.1'
6
6
ms.author: riande
7
7
ms.custom: mvc
8
-
ms.date: 12/18/2019
8
+
ms.date: 03/17/2020
9
9
no-loc: [Blazor, SignalR]
10
10
uid: blazor/routing
11
11
---
@@ -190,16 +190,16 @@ The following HTML markup is rendered:
190
190
191
191
## URI and navigation state helpers
192
192
193
-
Use `Microsoft.AspNetCore.Components.NavigationManager` to work with URIs and navigation in C# code. `NavigationManager` provides the event and methods shown in the following table.
193
+
Use <xref:Microsoft.AspNetCore.Components.NavigationManager> to work with URIs and navigation in C# code. `NavigationManager` provides the event and methods shown in the following table.
194
194
195
195
| Member | Description |
196
196
| ------ | ----------- |
197
-
|`Uri`| Gets the current absolute URI. |
198
-
|`BaseUri`| Gets the base URI (with a trailing slash) that can be prepended to relative URI paths to produce an absolute URI. Typically, `BaseUri` corresponds to the `href` attribute on the document's `<base>` element in *wwwroot/index.html* (Blazor WebAssembly) or *Pages/_Host.cshtml* (Blazor Server). |
199
-
|`NavigateTo`| Navigates to the specified URI. If `forceLoad` is `true`:<ul><li>Client-side routing is bypassed.</li><li>The browser is forced to load the new page from the server, whether or not the URI is normally handled by the client-side router.</li></ul> |
200
-
|`LocationChanged`| An event that fires when the navigation location has changed. |
201
-
|`ToAbsoluteUri`| Converts a relative URI into an absolute URI. |
202
-
|`ToBaseRelativePath`| Given a base URI (for example, a URI previously returned by `GetBaseUri`), converts an absolute URI into a URI relative to the base URI prefix. |
197
+
| Uri | Gets the current absolute URI. |
198
+
| BaseUri | Gets the base URI (with a trailing slash) that can be prepended to relative URI paths to produce an absolute URI. Typically, `BaseUri` corresponds to the `href` attribute on the document's `<base>` element in *wwwroot/index.html* (Blazor WebAssembly) or *Pages/_Host.cshtml* (Blazor Server). |
199
+
| NavigateTo | Navigates to the specified URI. If `forceLoad` is `true`:<ul><li>Client-side routing is bypassed.</li><li>The browser is forced to load the new page from the server, whether or not the URI is normally handled by the client-side router.</li></ul> |
200
+
| LocationChanged | An event that fires when the navigation location has changed. |
201
+
| ToAbsoluteUri | Converts a relative URI into an absolute URI. |
202
+
|<spanstyle="word-break:normal;word-wrap:normal">ToBaseRelativePath</span>| Given a base URI (for example, a URI previously returned by `GetBaseUri`), converts an absolute URI into a URI relative to the base URI prefix. |
203
203
204
204
The following component navigates to the app's `Counter` component when the button is selected:
205
205
@@ -220,3 +220,34 @@ The following component navigates to the app's `Counter` component when the butt
220
220
}
221
221
}
222
222
```
223
+
224
+
The following component handles a location changed event. The `HandleLocationChanged` method is unhooked when `Dispose` is called by the framework. Unhooking the method permits garbage collection of the component.
<xref:Microsoft.AspNetCore.Components.Routing.LocationChangedEventArgs> provides the following information about the event:
249
+
250
+
*<xref:Microsoft.AspNetCore.Components.Routing.LocationChangedEventArgs.Location>– The URL of the new location.
251
+
*<xref:Microsoft.AspNetCore.Components.Routing.LocationChangedEventArgs.IsNavigationIntercepted>– If `true`, Blazor intercepted the navigation from the browser. If `false`, [NavigationManager.NavigateTo](xref:Microsoft.AspNetCore.Components.NavigationManager.NavigateTo%2A) caused the navigation to occur.
252
+
253
+
For more information on component disposal, see <xref:blazor/lifecycle#component-disposal-with-idisposable>.
0 commit comments