Slide 1

Slide 1 text

Angular without Modules - A Dive into Standalone Components By Maina Wycliffe

Slide 2

Slide 2 text

About Me Software Engineer, Typescript aficionado @mwycliffe_dev This is Learning @Thisis_Learning Author of the All Things Typescript Newsletter - allthingstypescript.dev Maina Wycliffe

Slide 3

Slide 3 text

NgModules?

Slide 4

Slide 4 text

- Basic Building Blocks of Angular - Containers for components, directives, pipes, etc. - Providing Dependency Injection - Imports and Export - Bootstrapping root component Why?

Slide 5

Slide 5 text

NgModules

Slide 6

Slide 6 text

Standalone Components

Slide 7

Slide 7 text

Demo bit.ly/391k56N Code bit.ly/3aFzs5f

Slide 8

Slide 8 text

- Affects Components, Pipes, Directives - Components that can be used without NgModules - In developer preview - Standalone property for standalone components - Angular 14 Feature What’s changing

Slide 9

Slide 9 text

app.component.ts @Component({ standalone: true, selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'], imports: [RouterModule, CommonModule], }) export class AppComponent {} Indicates a component is a Standalone component

Slide 10

Slide 10 text

date-time.pipe.ts @Pipe({ name: 'dateTime', standalone: true, }) export class DateTimePipe implements PipeTransform { transform(value: unknown, ...args: unknown[]): string | null { // ... } } Indicates a pipe is a Standalone pipe

Slide 11

Slide 11 text

complete-strike-through.directive.ts @Directive({ selector: '[appCompleteStrikeThrough]', standalone: true }) export class CompleteStrikeThroughDirective implements OnInit, OnChanges { // ... } Indicates a directive is a Standalone component

Slide 12

Slide 12 text

New projects in v14 will still be generated using with NgModules ng new is not changing

Slide 13

Slide 13 text

- Delete the root module - app.module.ts - Modify main.ts and replace bootstrapModule() with bootstrapApplication() Migrating to Standalone Components

Slide 14

Slide 14 text

importProvidersFrom - Extracts providers from the provided module - The providers are made available to the root component and it’s children

Slide 15

Slide 15 text

bootstrapApplication List of providers that should be available to the root component and all its children.

Slide 16

Slide 16 text

- Finally, modify app.component to add the standalone: true property

Slide 17

Slide 17 text

New Components Using the ng generate with --standalone flag to generate standalone components i.e. ng g component component-name --standalone ng g directive directive-name --standalone ng g pipe pipe-name --standalone

Slide 18

Slide 18 text

Using Standalone Components - Import standalone component, directive, pipe - Imports – property can also import other modules - providers - property for Dependency Injection

Slide 19

Slide 19 text

Using Standalone Components

Slide 20

Slide 20 text

Routing Nothing changes in terms of routing

Slide 21

Slide 21 text

Lazy Loading Use loadComponent property to lazy load a component instead of loadChildren

Slide 22

Slide 22 text

What’s next? - NgModules are not going anywhere - You can use a combination of both

Slide 23

Slide 23 text

github.com/mainawycliffe/angular-without-modules-google-io-extended-2022-demo DEMO

Slide 24

Slide 24 text

Thank you! Software Engineer @mwycliffe_dev mainawycliffe.dev mainawycliffe Maina Wycliffe