Lavkush Gupta
Lavkush Gupta

Reputation: 103

Angular 4 execute function from another component without using Services

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

Upvotes: 0

Views: 549

Answers (2)

Vikas Garg
Vikas Garg

Reputation: 200

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.

Upvotes: 1

erni
erni

Reputation: 176

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());
 }
}

Upvotes: 1

Related Questions