简体   繁体   English

在 angular 10 中将变量从一页传递到另一页

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

Hi I am trying to pass a variable from one component to another, I tried the following:您好我正在尝试将变量从一个组件传递到另一个组件,我尝试了以下操作:

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

Now in my other page I want to receive it but just in the ts file and not the html as it will be used for internal logic.现在在我的另一个页面中,我想接收它,但只是在 ts 文件中,而不是在 html 中,因为它将用于内部逻辑。

I tried this but it did not work:我试过了,但没有奏效:

 @Input() userFlow: boolean;

Following approach might be useful for you!以下方法可能对您有用!

In the receiver component typescript, you have to create a method to receive the value of the userFlow variable like this:接收器组件 typescript 中,您必须创建一个方法来接收 userFlow 变量的值,如下所示:

export class ReceiverComponent {

constructor() { }

userFlow:boolean;

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

In receiver component html put the following:接收器组件 html 中放入以下内容:

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

In the sender component typescript ( app-sender selector), you should declare a booleanValueEvent variable with the @Output() decorator and set it equal to a new event emitter.发送方组件 typescript( app-sender选择器)中,您应该使用 @Output() 装饰器声明一个 booleanValueEvent 变量并将其设置为新的事件发射器。

  export class SenderComponent {

  userFlow: boolean = true;

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

  constructor() { }

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

In sender component.ts file you can add在发件人 component.ts 文件中,您可以添加

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

In receiver component.ts file add在接收器 component.ts 文件中添加

userFlow = history.state.data;

Make sure to import router确保导入路由器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM