A simpler Angular component inheritance
Related to this documentation in Angular website:
https://angular.dev/guide/components/inheritance
Simplifying inheritance by removing the need for:
- having a constructor in the parent just for common dependency injection
- having a constructor in the child just for common dependency injection
- calling super for common dependency injection
- calling super for common lifecycle hooks
Stackblitz link:
https://stackblitz.com/edit/stackblitz-starters-tzhggu9s?file=src%2Fmain.ts
Parent component:
@Component({template:``})abstractclassParent{protectedheroService=inject(HeroService);ngOnInit(){this.onInit();}protectedabstractonInit():void;}
Child component:
@Component({selector:'child',template:`Child`,})classChildextendsParent{overrideonInit(){this.heroService.log();}}
App:
@Component({selector:'app-root',template:`<child/>`,imports:[Child],})classApp{}bootstrapApplication(App);
Common service:
@Injectable({providedIn:'root'})classHeroService{log(){console.log('heroService');}}
Advice is to have:
{"compilerOptions":{"noImplicitOverride":true,}}
in tsconfig if not already there.
Goal is to avoid dev to mistakenly write an ngOnInit without knowing they'd be overriding the parent one.
Top comments(0)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse