4

I am creating a v2 for my UI. I want to keep the old UI and make changes. So I was thinking of keeping the name of new component same as old one.

So it is possible to have 2 different component with same name?

2
  • You mean Same Selector name or class name ? can you provide some examples ? Commented May 10, 2018 at 23:08
  • both the selector and class names. but having same selector might create some conflict Commented May 11, 2018 at 17:33

2 Answers 2

7

What you could do is have two Classes one old and one new, with semantic naming rather than new and old of course, and within the master component class, have the logic for switching between the two.

or you could import as Alias, which means both classes can be the same name but where you use it obviously has to be different:

import { MyComponent as OldComponent } from '../old/my-component'
import { MyComponent} from '../new/my-component'
Sign up to request clarification or add additional context in comments.

Comments

1

The best course of action to create a v2 would be to either use version control to branch from your old UI or to refactor your app to use your new UI instead. Under no circumstances I would recommend to create components with the same name especially when they serve the same purpose. It would only be confusing to yourself as well as other developers.

With that being said, if you need components to share a common logic. I would suggest you make your components inherit a base class:

export abstract class BaseComponent implements OnInit {
    ngOnInit() {
        // Example
    }
}

@Component({
    selector: 'a-selector'
})
export class NewComponent extends BaseComponent {
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.