Angular 9 has just been released and includes a number of major improvements. Be sure to check the official announcement to learn more. If like me you build single page apps with ASP.NET Core and Angular you will be keen to upgrade. This post details the steps required to upgrade the .NET Core project template to Angular 9.
Prerequisites
Ensure that you meet the following prerequisites:
- .NET Core SDK (3.1 or later)
- Node.js (10.13 or later)
Creating a new project
Create a Angular .NET Core SPA project from the command-line by running:
The ASP.NET Core project provides an API back end and the Angular CLI project (contained within ClientApp) provides the front end / SPA.
The output argument specifies the output directory name, and, since no name argument is specified, it is also the name for the new project.
Authentication can be added using the optional auth argument. Specifying Individual creates the project using ASP.NET Core Identity for authenticating and storing users combined with IdentityServer for implementing OIDC.
Build the new solution using the following commands:
The above command restores NuGet / npm dependencies (takes a few minutes) and then builds the entire project.
Next, create a new git repository and commit the initial version using the following commands:
In the next section, you will upgrade the project to Angular 9.
Upgrade to Angular 9
The process is relatively straightforward, aside from a couple of issues we will cover later. First, switch to the Angular SPA directory (ClientApp):
Next, run ng update
to update to the latest 8.x version and then commit those changes:
Next, run ng update
again to upgrade to Angular 9 and then commit those changes:
If everything was successful, you will see the following message:
Don’t get too excited, we are not done yet. You will receive the following error if you attempt to build the Angular project:
You can learn more about this issue here. However to resolve this issue, simply remove the following line from ClientApp/src/main.ts:
Finally, run ng build
and, if successful, commit your changes:
No further steps required! In the next section, you will launch the project to verify the upgrade was successful.
Launching the project
Normally launching the project is as easy as executing dotnet run
, however, if you do so now, you will see the following error:
You can learn more about this issue here. Fortunately, it is easy to resolve this issue. First, open ClientApp/package.json and locate this line:
Then change to include an echo
command as follows:
Easy. Be sure to commit these changes too:
Now launch the project from the root directory using dotnet run
. The project will build, start the front and back end, and then display the following message:
Browse to https://localhost:5001 to see the home page of the new project:
That’s it! You are ready to get started developing with Angular 9 and ASP.NET Core 3.1. Thanks for reading, be sure to take a look at some of the other posts in this category. If you did run into any issues, view the sample code for this post. If you can’t figure it out from there, please post a comment below.
No comments:
Post a Comment