2

Angular 15 - Standalone Components Are Stable

 1 year ago
source link: https://www.infoq.com/news/2022/11/angular-15-standalone-components/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Angular 15 - Standalone Components Are Stable

Nov 21, 2022 1 min read

Google recently released Angular 15, the latest version of their popular SPA framework. The update includes a stable API for standalone components alongside several other significant improvements.

Standalone components are long-awaited features that enable developers to build Angular applications without using Modules.

It was previewed in Angular 14, and after some adjustments following community feedback, it's now reached a stable state and is ready for broad adoption.

The standalone API largely remains the same and consists of a new 'standalone' property that can be added to the existing Component decorator.

The CLI has been updated to support standalone component creation using the --standalone flag.

ng g component --standalone

bootstrapApplication is then used to bootstrap the Angular application using the root standalone component.

import {bootstrapApplication} from '@angular/platform-browser';

@Component({
  standalone: true,
  selector: 'demo-component',
  template: `…`,
})
export class DemoComponent {
  // component logic
}

bootstrapApplication(DemoComponent);

It is now also possible to build multi-route applications using standalone components by providing the router with the bootstrapApplication function.

bootstrapApplication(AppComponent, {
  providers: [
    provideRouter(appRoutes)
  ]
});

Angular 15 also includes two significant unrelated improvements.

The directive composition API has been requested since Angular 2 was first released. It allows applying directives to a component's host element from within the component.

@Component({
    selector: 'demo-comopenent',
    hostDirectives: [DemoDirective]
})

In addition, the NgOptimizedImage directive that was previewed in Angular 14.2 is now ready for wide adaptation.

According to the Angular team, it shows promising results in improving image load performance by enabling lazy loading of images and encouraging developers to adopt best practices.

Finally, the esbuild integration that was started in Angular 14 received several improvements, including support for Sass, SVG template files, file replacement, and the --watch flag.

While the integration is still flagged as experimental, initial tests have shown double-digit improvement in build times.

Developers are encouraged to experiment with the new build by updating the builder property in angular.json to:

"builder": "@angular-devkit/build-angular:browser-esbuild"

As with every experimental feature, developers are advised to take the necessary precautions before using it in production.

For a complete list of changes, head to the official release announcement

Angular is open-source software available under the MIT license. Contributions are welcome via the Angular GitHub repository.

About the Author

Guy Nesher

Developer at Locusview focusing on web technologies and active speaker/meetup organizer.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK