0

i have a login component, In this component i'm using Dialog Box for login dynamically which is also accessible from other component. In this component we have logout function (to logout user). so how i can execute logout function form other component which is present in Login component.

export class loginComponent implements OnInit {

let postdata = {
     email:email,
     password:password
 }

  this.register.customerLogin(postdata).subscribe( (response) => { response; });

}

logout() {

 localStorage.removeItem('cemail');
 localStorage.removeItem('cname');
 localStorage.removeItem('cid');
 localStorage.removeItem('token');

}

Thanks

5
  • 3
    Drop the "without using Services" from your arbitrary requirements, since a service is precisely what you should use. Commented Apr 3, 2018 at 6:34
  • Exactly! Use the service Commented Apr 3, 2018 at 6:35
  • @DavidVotrubec Thanks for ans. Is any other way to do this ? Commented Apr 3, 2018 at 6:39
  • Yes, you can use custom events which will bubble up the DOM and then react to those events, but it is more work and it depends on how your components are related to each other in the DOM. So the service is the easiest and recommended way Commented Apr 3, 2018 at 6:40
  • Thanks @DavidVotrubec Commented Apr 3, 2018 at 6:44

2 Answers 2

1

Services are better but if you want execute child component methods you can use @ViewChild()

export class AppComponent implements AfterViewInit {

 @ViewChild(ChildComponent) child: ChildComponent;

 ngAfterViewInit() {
  console.log(this.child.myMethod());
 }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Services are better option but you can use @Output and EventEmitter to create a event and emit from child to parent and pass data from parent to child using @Input.

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.