Can I have multiple components attached to one class object in Angular 2?

I have two different templates and styling for an input box, but they basically function the same way and access the back-end data the same way. I would like the two templates to access the same functions. Is there a way to attach 2 components to one class or allow one component to access the other’s functions?

@Component({
    selector: 'inputbox1',
    templateUrl: 'templates/Tnputbox1.html'
})
export class Inputbox1 { 
...
}

The other option is to create two classes that have identical code for each component, or rewrite all the templates and styling.

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

You can create an Abstract Class and extend it for other classes :

@Component({
    selector: 'inputbox1',
    templateUrl: 'templates/Tnputbox1.html'
})
export class Inputbox1 extends Inputbox { 
...
}

@Component({
    selector: 'inputbox2',
    templateUrl: 'templates/Tnputbox2.html'
})
export class Inputbox2 extends Inputbox { 
...
}

abstract class Inputbox { 
...your common functions
}