This context provides a guide on upgrading an Angular application from version 15 to 16 and converting Angular components to standalone type using Angular CLI.
Abstract
The context discusses the process of upgrading an Angular application from version 15 to 16, which includes the necessary package.json requirements. It also introduces the concept of standalone components in Angular and explains how to use Angular CLI to automatically convert Angular components to standalone type. The author shares their experience of upgrading a project and provides a supplementary screencast for step-by-step guidance. The context also includes code snippets and a link to the upgraded Angular 16 source code on GitHub.
Bullet points
The context provides a guide on upgrading an Angular application from version 15 to 16.
The necessary package.json requirements for upgrading are discussed.
The concept of standalone components in Angular is introduced.
Angular CLI is used to automatically convert Angular components to standalone type.
The author shares their experience of upgrading a project.
A supplementary screencast is provided for step-by-step guidance.
Code snippets are included in the context.
The upgraded Angular 16 source code is available on GitHub.
Upgrading Angular 15 to 16: A Full-stack Web Development Project using Angular and .NetCore WebAPI
Introduction
Angular version 16, which was released on May 3rd, 2023, includes a new feature called Schematics for Standalone Components. This feature allows developers to improve our documentation and schematics and helps us create standalone components for your Angular applications. These components are not tied to any specific module and can be used anywhere in your application. Standalone components are useful for developing reusable UI elements or libraries.
In this blog post, I will share my experience of upgrading an Angular application from version 15 to 16, and the automated approach I took to convert all the Angular components of the Talent Management project from the Fullstack Angular 15, Bootstrap 5 & NET 7 API blog series to standalone type. You can find the upgraded Angular 16 source code of the project on GitHub.
With the new Angular CLI command ng generate @angular/core:standalone, I effortlessly transformed the entire project into standalone components in just an hour. I strongly encourage you to give it a shot. It’s definitely worth it.
— Fuji Nguyen
Screencast
Since there is a lot of information to cover, I created a supplementary screencast to provide step-by-step guidance on upgrading the project.
Part 1 — Upgrading an Angular application from version 15 to 16
Before the upgrade, make sure you have
Node.js versions: v16 and v18.
TypeScript version 4.9.3 or later.
Zone.js version 0.13.x or later.
If you are using Angular CLI, you can run the following command to update your project:
ng update@angular/core @angular/cli
At the time of this writing, there are a few 3rd party libraries that are not compatible with Angular 16 as shown in the screenshot below.
The workaround is to run with -- force option:
ng update@angular/core @angular/cli --force
Below is the package.json that lists the version of Angular and 3rd party libraries. Please notice that when running npm -i, the ng-bootstrap/ng-bootstrap may report an upstream dependency conflict. To overcome this warning, you can run npm -i --force.
The package.json file is used in a Node.js project to manage dependencies, scripts, and other project metadata. The name field specifies the name of the project, and the version field specifies the version of the project.
The dependencies field lists the dependencies that the project relies on. Each dependency is specified as a key-value pair, where the key is the name of the package, and the value is the version range that should be used. For example, @angular/* specifies a range of ~16.0.1, which means that any version of the package that is compatible with the version 16.0.1 will be used.
After upgrading, I attempted to run ng serve, but it failed due to an incompatibility issue with the parameter suppressImplicitAnyIndexErrors in the tsconfig.json file. To resolve this issue, I removed this parameter and was then able to successfully run the project without any problems. See the screenshot below for more info.
Part 2: Upgrading Angular components to standalone type
Angular 16 contains improved documentation and schematics for standalone components. To support developers transitioning their apps to standalone APIs, the Angular team developed migration schematics and a standalone migration guide. Once you’re in your project directory run:
ng generate @angular/core:standalone
The command will prompt you to choose the type of migration as shown in the screenshot below:
I chose to Bootstrap the application using standalone APIs, which removes the need for the app.modules.ts file. The application is bootstrapped from main.ts using the AppComponent. See below for the source code of main.ts.
This main.ts is the entry point of the Angular application, which bootstraps the AppComponent and initializes the application’s modules, services, and dependencies. It includes imports for various Angular modules, third-party libraries, and custom modules that define the application’s structure.
After converting the code to bootstrap the app using AppComponent, I then re-ran the command ng generate @angular/core:standalone and this time I chose the option Covert all components, directives and pipes to standalone as shown in the screenshot below.
This time, it converted all the components in the project to standalone components. Below is an example of the converted app.component.ts.
import { Component, OnInit, OnDestroy } from'@angular/core';
import { Router, NavigationEnd, ActivatedRoute, RouterOutlet } from'@angular/router';
import { Title } from'@angular/platform-browser';
import { TranslateService } from'@ngx-translate/core';
import { merge } from'rxjs';
import { filter, map, switchMap } from'rxjs/operators';
import { environment } from'@env/environment';
import { Logger, UntilDestroy, untilDestroyed } from'@shared';
import { I18nService } from'@app/i18n';
import { ToastsContainer } from'./@shared/toast/toasts-container.component';
const log = newLogger('App');
@UntilDestroy()
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
standalone: true,
imports: [RouterOutlet, ToastsContainer],
})
exportclassAppComponentimplementsOnInit, OnDestroy {
constructor(private router: Router,
private activatedRoute: ActivatedRoute,
private titleService: Title,
private translateService: TranslateService,
// do not remove the analytics injection, even if the call in ngOnInit() is removed// this injection initializes page tracking through the routerprivate i18nService: I18nService
) {}
ngOnInit() {
// Setup loggerif (environment.production) {
Logger.enableProductionMode();
}
log.debug('init');
// Setup translationsthis.i18nService.init(environment.defaultLanguage, environment.supportedLanguages);
const onNavigationEnd = this.router.events.pipe(filter((event) => event instanceofNavigationEnd));
// Change page title on navigation or language change, based on route datamerge(this.translateService.onLangChange, onNavigationEnd)
.pipe(
map(() => {
let route = this.activatedRoute;
while (route.firstChild) {
route = route.firstChild;
}
return route;
}),
filter((route) => route.outlet === 'primary'),
switchMap((route) => route.data),
untilDestroyed(this)
)
.subscribe((event) => {
const title = event['title'];
if (title) {
this.titleService.setTitle(this.translateService.instant(title));
}
});
}
ngOnDestroy() {
this.i18nService.destroy();
}
}
This is the source code for the root component of the Angular application, AppComponent. The AppComponent class defines the HTML template and styles for the root component, which includes a router outlet and a toast container component. Notice that the standalone: true which indicates this is a standalone component.
Source Code
You can find the upgraded Angular 16 source code of the project on GitHub.
Demo screenshot: Homepage with a listing of login accounts
I have leveraged the power of GitHub Actions to set up a demo of the Angular 16 client on the GitHub Page. Additionally, I have seamlessly integrated the backend ApiResource and Token Service in Azure. To witness the demo in action, simply click the link provided below:
Demo screenshot: Pagination, Filter, and Detail Modal Popup from the Employee menu
This integration between GitHub Actions and Azure ensures a smooth and efficient deployment process, allowing you to experience the Angular client along with the associated backend services. Take this opportunity to explore the demo and discover the seamless collaboration between the front-end and back-end components. Follow the link to access the demo and witness the power of this combined solution.
In this blog, we explored upgrading Angular from version 15 to 16, including the necessary package.json requirements. We also learned how to use Angular CLI to automatically convert Angular components to standalone type, making them more flexible and loosely coupled.
When I get bitten by insects, one part of my brain is like “be smart, leave it alone”. The other part is like…
“Scratch that” ☹
The standalone component concept in Angular reminds me of user controls in the old .NET Framework’s Web Forms. User control is self-contained and can be called from many ASP.NET web pages, which is similar to how a standalone component can be used across multiple Angular modules. I find this approach to be more loosely coupled and flexible, and I really appreciate it in Angular.
Thanks for reading! Hope you found it useful. Want more? Please follow me and become a member on Medium for more articles. With your support, I’ll keep creating awesome content for you. Have a great day ahead!
— Fuji Nguyen