Gili Yaniv
Gili Yaniv

Reputation: 3221

create reusable component in angular 2

I would like to crate a 'login component' that display some kind of popup modal. Consider I've many places all over my app that can call this modal,What is a batter approach: 1. To place the 'login component' everywhere I use it although its the same everywhere. 2. Place it in top level component and show and hide it with some kind of event listener.

In the first way I encapsulate the component to display what it needs and have an easy control of its life cycle. In the second way I avoid repeating my self and call it every components.

Thanks.

Upvotes: 1

Views: 612

Answers (2)

Sukhdeep Singh
Sukhdeep Singh

Reputation: 41

You don't need to do any of this. Just create a single login component and redirect to it whenever you need to. Do not repeat yourself. Have some way of validating user's login status. Then whenever login status goes invalid, just redirect to the login page with appropriate messages

Upvotes: 1

Günter Zöchbauer
Günter Zöchbauer

Reputation: 658077

The better way is to place it somewhere once and use a shared service with observables to allow all interested components to send messages.

See also https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service

Upvotes: 1

Related Questions