Let below is a component which I will be extending in various other components ti reuse some code..
import { Component } from '@angular/core';
@Component ({
selector: 'my-app',
template: ` <div>
<h1>{{appTitle}}</h1>
<div>To Tutorials Point</div>
</div> `,
})
export class AppComponent {
appTitle: string = 'Welcome';
ngOnInit(){
this.registerSomeSubscriptions();
}
registerSomeSubscriptions(){
this.subscribeSomething.subscribe((data)=>{
performSomeAction();
})
}
}
I can extend it like below
import { Component } from '@angular/core';
@Component ({
selector: 'my-app',
template: ` <div>
<h1>{{appTitle}}</h1>
<div>To Tutorials Point</div>
</div> `,
})
export class ChildComponent extends AppComponent {
}
While I know that public members of the component will be available in the child component.
My Question
The Child components can either use the same html template as the parent Component or they can have their own html files, in which case the html of the parent component will not be used.
If you want to use the parent component's html with some modification in the Child components you can reuse the parent component instead of inheriting it.
// add something specific to child/feature component here
<app-reusable-component></app-reusable-component>
// add something specific to child/feature component here
The reusable component's html will be available along with the additional html that you add in the feature component's html.
to pass the data between the reusable component and the feature component use component interaction - Input
and Output
.
Input
- Pass data from feature component to reusable component with input binding
reusable-component.ts:
export class ReusableComponent implements OnInit {
@Input() dataIn: Observable<object>;
ngOnInit() {
dataIn.subscribe(data => {
// display data
});
}
}
feature-component.html:
...
<app-reusable-component [dataIn]="dataToDisplay$"></app-reusable-component>
...
feature-component.ts:
export class FeatureComponent implements OnInit {
public dataToDisplay$: Observable<object>;
ngOnInit() {
this.dataToDisplay$ = this.http.get(url);
}
}
Output
- Send data/events from reusable component to feature componentreusable-component.ts:
export class ReusableComponent implements OnInit {
@Input() dataIn: Observable<object>;
@Output() eventOut: EventEmitter<object> = new EventEmitter<object>();
ngOnInit() {
dataIn.subscribe(data => {
// display data
});
}
userInput(value) {
this.eventOut.emit(value);
}
}
feature-component.html:
...
<app-reusable-component [dataIn]="dataToDisplay$" (eventOut) ="handleUserData($event)"></app-reusable-component>
...
feature-component.ts:
export class FeatureComponent implements OnInit {
public dataToDisplay$: Observable<object>;
ngOnInit() {
this.dataToDisplay$ = this.http.get(url);
}
handleUserData(value) {
this.http.post(url, value);
}
}
In this approach you can reuse the template of the parent component in all your child/feature components, and you will be able add additional html content in your child/feature components and pass data between them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With