Manuel
Manuel

Reputation: 11489

Angular 2 multiple components with the same selector

How do you deal with the following situation where two components use the same selector? (removed the word "Component" to avoid clutter):

app.ts

import { Component } from '@angular/core';
import { Person as Person1 } from './person1';
import { Person as Person2 } from './person2';
@Component({
  selector: 'app',
  template: '<person1???></person1><person2???></person2>',
  directives: [Person1, Person2]
})
export class App {

}

person1.ts

import { Component } from '@angular/core';
@Component({
  selector: 'person',
  template: '<h1>Person 1</h1>'
})
export class Person {

}

person2.ts

import { Component } from '@angular/core';
@Component({
  selector: 'person',
  template: '<h1>Person 2</h1>'
})
export class Person {

}

This isn't a problem with React since you'd just use <Person1 /> and <Person2 />

Upvotes: 4

Views: 14248

Answers (1)

Manuel
Manuel

Reputation: 11489

Based on Günter Zöchbauer comment you can add a bunch of boilerplate to accomplish this and wrap the components:

app.ts

import { Component } from '@angular/core';
import { Person as Person1 } from './person1';
import { Person as Person2 } from './person2';

@Component({
  selector: 'person1',
  template: '<person></person>',
  directives: [Person1]
})
export class Person1Wrapper {

}

@Component({
  selector: 'person2',
  template: '<person></person>',
  directives: [Person2]
})
export class Person2Wrapper {

}

@Component({
  selector: 'app',
  template: '<person1></person1><person2></person2>',
  directives: [Person1Wrapper, Person2Wrapper]
})
export class App {

}

Upvotes: 1

Related Questions