簡體   English   中英

在 angular 10 中將變量從一頁傳遞到另一頁

[英]pass variable from one page to another in angular 10

您好我正在嘗試將變量從一個組件傳遞到另一個組件,我嘗試了以下操作:

  @Output() public userFlow = new EventEmitter<boolean>();
  this.userFlow.emit(this.userFlowThrough);

現在在我的另一個頁面中,我想接收它,但只是在 ts 文件中,而不是在 html 中,因為它將用於內部邏輯。

我試過了,但沒有奏效:

 @Input() userFlow: boolean;

以下方法可能對您有用!

接收器組件 typescript 中,您必須創建一個方法來接收 userFlow 變量的值,如下所示:

export class ReceiverComponent {

constructor() { }

userFlow:boolean;

receiveBooleanValue($event) {
  this.userFlow= $event
 }
}

接收器組件 html 中放入以下內容:

<app-sender (booleanValueEvent)="receiveBooleanValue($event)"></app-sender>
<h1>{{userFlow}}<h1>

發送方組件 typescript( app-sender選擇器)中,您應該使用 @Output() 裝飾器聲明一個 booleanValueEvent 變量並將其設置為新的事件發射器。

  export class SenderComponent {

  userFlow: boolean = true;

  @Output() booleanValueEvent = new EventEmitter<boolean>();

  constructor() { }

  ngOnInit(): void {
    this.booleanValueEvent.emit(this.userFlow)
  }
}

在發件人 component.ts 文件中,您可以添加

sendData(){
    this.router.navigate(['receiverComponent'],{state:{data:this.userFlow}})
}

在接收器 component.ts 文件中添加

userFlow = history.state.data;

確保導入路由器

暫無
暫無

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

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