簡體   English   中英

Angular 4在不使用服務的情況下從另一個組件執行功能

[英]Angular 4 execute function from another component without using Services

我有一個登錄組件,在此組件中,我正在使用對話框動態登錄,也可以從其他組件訪問該對話框。 在此組件中,我們具有注銷功能(用於注銷用戶)。 因此,我如何從登錄組件中存在的其他組件執行注銷功能。

導出類loginComponent實現OnInit {

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

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

}

登出() {

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

}

謝謝

服務更好,但如果要執行子組件方法,則可以使用@ViewChild()

export class AppComponent implements AfterViewInit {

 @ViewChild(ChildComponent) child: ChildComponent;

 ngAfterViewInit() {
  console.log(this.child.myMethod());
 }
}

服務是更好的選擇,但是您可以使用@Output和EventEmitter創建一個事件,並從子對象向父對象發送事件,並使用@Input將數據從父對象傳遞給子對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM