Using the default projects generated by the Angular CLI and .NET Core CLI, this project shows how to integrate Blazor WASM components to an Angular project
- Download and Install Node
- Download and Install .NET 6 SDK
- Note: These samples work with with Angular v12 and Node v12. If need be use node version manager (nvm) to set to v12
See Installing Angular CLI to get started with a simple Angular project
ng new simple-angular-project
cd simple-angular-project
ng serve
See Tooling for ASP.NET Core Blazor to get started with a simple Blazor project
dotnet new blazorwasm --name simple-blazor-project
cd simple-blazor-project
dotnet run
dotnet add package Microsoft.AspNetCore.Components.Web --version 6.0.0
dotnet add reference ../js-component-generation-helpers/JSComponentGeneration/JSComponentGeneration.csproj
dotnet add reference ../js-component-generation-helpers/JSComponentGeneration.Build/JSComponentGeneration.Build.csproj
Unfortunately there is no CLI command for this step so it needs to be added manually by editing the simple-blazor-project.csproj
<!--Add this just before the closing </Project> tag-->
<Import Project="..\js-component-generation-helpers\JSComponentGeneration.Build\build\netstandard2.0\JSComponentGeneration.build.targets" />
This step will create a standard Blazor Component and mark it with an Attribute tat it be generated as an Angular component using the GenerateAngular
attribute
dotnet new razorcomponent --name BlazorInfo
- Relace the BlazorInfo.razor code with the following
@using JSComponentGeneration.Angular
@attribute [GenerateAngular]
<h3>Blazor Info Component</h3>
<p>@Message</p>
@code {
[Parameter] public string Message { get; set; } = "Default Message";
}
- Add the following using clause to simple-blazor-project/Program.cs
using JSComponentGeneration.Angular;
- Add the following line to the builder
builder.RootComponents.RegisterForAngular<BlazorInfo>();
From the simple-blazor-project folder
dotnet build
Add the newly generated components to app.module.ts by addign the following lines:
import { BlazorInfoComponent } from "./blazor-info/blazor-info.component";
In the declarations section:
BlazorInfoComponent
In index.html add the following script above the closing body
tag
<script src="_framework/blazor.webassembly.js"></script>
Create a file proxy.conf.json and the angular src folder
{
"/": { "target": "https://localhost:7025/", "secure": false }
}
Note that
"secure"
values is false above otherwise you will get a DEPTH_ZERO_SELF_SIGNED_CERT error while proxying the request
Add the <blazor-info>
tag to the app.component.html. Add the following block of html
<div>
<button (click)="toggleBlazorComponentVisibility()">Toggle Blazor Component</button>
<div *ngIf="blazorComponentVisible">
<blazor-info [message]="title"></blazor-info>
</div>
</div>
In the app.component.html, code up the blazorComponentVisible
boolean and toggleBlazorComponentVisibility()
method by modifying the AppComponent Class to this:
export class AppComponent{
title = 'simple-angular-project';
blazorComponentVisible: boolean;
constructor() {
this.blazorComponentVisible = false;
}
toggleBlazorComponentVisibility() {
this.blazorComponentVisible = !this.blazorComponentVisible
}
}
From the /src folder:
dotnet new sln --name simple-blazor-project
dotnet sln add simple-blazor-project/simple-blazor-project.csproj
dotnet sln add js-component-generation-helpers/JSComponentGeneration/JSComponentGeneration.csproj
dotnet sln add js-component-generation-helpers/JSComponentGeneration.Build/JSComponentGeneration.Build.csproj
Further Notes: libman.json is not required
Final Output: