Skip to content

Commit f25788d

Browse files
Merge pull request santoshyadavdev#4 from olierxleben/snippet-v15-standalone
snippet: added my very first snippet about standalone components
2 parents b403013 + d6969dc commit f25788d

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: Angular 15 Standalone Components
3+
description: "Easy example of Angular 15 standalone components in existing projects"
4+
pubDate: Feb 22, 2022
5+
contributedBy: "@olierxleben"
6+
---
7+
8+
Here is a quick example of how to use a standalone component in an existing NgModule. It demonstrates the interoperability and recent innovation of Angular v15.
9+
Let`s start with the component itself.
10+
11+
```typescript
12+
import {Component} from '@angular/core';
13+
14+
@Component({
15+
selector: 'fancy-button',
16+
standalone: true, // magic lies here
17+
18+
template: `
19+
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
20+
<ng-content></ng-content>
21+
</button>
22+
`,
23+
})
24+
export class ButtonComponent {
25+
}
26+
```
27+
---
28+
And for the integration in an NgModule, you can just do the following:
29+
30+
```typescript
31+
import {ButtonComponent} from "my-fancy-ui";
32+
...
33+
@NgModule({
34+
declarations: [AppComponent],
35+
imports: [
36+
BrowserModule,
37+
HttpClientModule, RouterModule.forRoot(routes), ButtonComponent], // import like a module
38+
providers: [],
39+
bootstrap: [AppComponent],
40+
})
41+
export class AppModule {
42+
}
43+
```

0 commit comments

Comments
 (0)