forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add a style recommendation for initializing inputs (angular#40698)
This practice is to better align with `strictPropertyInitialization` which is coming by default with strict mode in v12. PR Close angular#40698
- Loading branch information
1 parent
980f6a4
commit 72a00dc
Showing
13 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
aio/content/examples/styleguide/src/05-18/app/app.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'sg-app', | ||
template: '<toh-hero-list></toh-hero-list>' | ||
}) | ||
export class AppComponent { } |
20 changes: 20 additions & 0 deletions
20
aio/content/examples/styleguide/src/05-18/app/app.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { BrowserModule } from '@angular/platform-browser'; | ||
import { RouterModule } from '@angular/router'; | ||
|
||
import { AppComponent } from './app.component'; | ||
import { HeroComponent, HeroListComponent } from './heroes'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
BrowserModule, | ||
RouterModule.forChild([{ path: '05-18', component: AppComponent }]) | ||
], | ||
declarations: [ | ||
AppComponent, | ||
HeroComponent, | ||
HeroListComponent | ||
], | ||
exports: [AppComponent] | ||
}) | ||
export class AppModule {} |
24 changes: 24 additions & 0 deletions
24
aio/content/examples/styleguide/src/05-18/app/heroes/hero-list/hero-list.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
import { Hero } from '../shared/hero.model'; | ||
|
||
@Component({ | ||
selector: 'toh-hero-list', | ||
template: ` | ||
<section> | ||
Our list of heroes: | ||
<toh-hero *ngFor="let hero of heroes"> | ||
</toh-hero> | ||
Total powers: {{totalPowers}}<br> | ||
Average power: {{avgPower}} | ||
</section> | ||
` | ||
}) | ||
export class HeroListComponent { | ||
heroes: Hero[] = []; | ||
totalPowers = 1; | ||
|
||
get avgPower() { | ||
return this.totalPowers / this.heroes.length; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
aio/content/examples/styleguide/src/05-18/app/heroes/hero-list/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './hero-list.component'; |
15 changes: 15 additions & 0 deletions
15
aio/content/examples/styleguide/src/05-18/app/heroes/hero/hero.component.avoid.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Component, Input } from '@angular/core'; | ||
|
||
// #docregion example | ||
@Component({ | ||
selector: 'toh-hero', | ||
template: `...` | ||
}) | ||
export class HeroComponent { | ||
// The exclamation mark suppresses errors that a property is | ||
// not initialized. | ||
// Ignoring this enforcement can prevent the type checker | ||
// from finding potential issues. | ||
@Input() id!: string; | ||
} | ||
// #enddocregion example |
17 changes: 17 additions & 0 deletions
17
aio/content/examples/styleguide/src/05-18/app/heroes/hero/hero.component.optional.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Component, Input } from '@angular/core'; | ||
|
||
// #docregion example | ||
@Component({ | ||
selector: 'toh-hero', | ||
template: `...` | ||
}) | ||
export class HeroComponent { | ||
@Input() id?: string; | ||
|
||
process() { | ||
if (this.id) { | ||
// ... | ||
} | ||
} | ||
} | ||
// #enddocregion example |
11 changes: 11 additions & 0 deletions
11
aio/content/examples/styleguide/src/05-18/app/heroes/hero/hero.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Component, Input } from '@angular/core'; | ||
|
||
// #docregion example | ||
@Component({ | ||
selector: 'toh-hero', | ||
template: `...` | ||
}) | ||
export class HeroComponent { | ||
@Input() id = 'default_id'; | ||
} | ||
// #enddocregion example |
1 change: 1 addition & 0 deletions
1
aio/content/examples/styleguide/src/05-18/app/heroes/hero/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './hero.component'; |
3 changes: 3 additions & 0 deletions
3
aio/content/examples/styleguide/src/05-18/app/heroes/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './hero'; | ||
export * from './hero-list'; | ||
export * from './shared'; |
4 changes: 4 additions & 0 deletions
4
aio/content/examples/styleguide/src/05-18/app/heroes/shared/hero.model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface Hero { | ||
id: number; | ||
name: string; | ||
} |
1 change: 1 addition & 0 deletions
1
aio/content/examples/styleguide/src/05-18/app/heroes/shared/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './hero.model'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './heroes'; | ||
export * from './app.component'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters