Angular 6:从一个组件传递id到另一个组件
创始人
2024-10-16 12:30:36
0

在Angular 6中,可以通过以下步骤从一个组件传递id到另一个组件:

  1. 首先,在接收id的组件(接收组件)中创建一个输入属性(Input property)来接收id。在接收组件的.ts文件中,添加以下代码:
import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-receiver',
  template: `
    

Received ID: {{ receivedId }}

` }) export class ReceiverComponent { @Input() receivedId: number; }
  1. 在发送id的组件(发送组件)中,使用@Output装饰器创建一个事件,并将id作为事件的参数。在发送组件的.ts文件中,添加以下代码:
import { Component, EventEmitter, Output } from '@angular/core';

@Component({
  selector: 'app-sender',
  template: `
    
  `
})
export class SenderComponent {
  @Output() idEvent = new EventEmitter();

  sendId() {
    const id = 123; // replace with your id
    this.idEvent.emit(id);
  }
}
  1. 在发送组件的父组件中(父组件),将接收id的组件(接收组件)添加到模板中,并通过事件绑定将id传递给接收组件。在父组件的.html文件中,添加以下代码:


  1. 在父组件的.ts文件中,创建一个方法来接收来自发送组件的id,并将其存储在父组件的变量中。添加以下代码:
import { Component } from '@angular/core';

@Component({
  selector: 'app-parent',
  template: `
    
    
  `
})
export class ParentComponent {
  receivedId: number;

  receiveId(id: number) {
    this.receivedId = id;
  }
}

这样,当点击发送组件中的按钮时,id将通过事件传递给父组件的方法,父组件将其存储在变量中,并将其传递给接收组件,接收组件将在模板中显示id。

请注意,上述代码只是一个示例,你需要根据你的实际需求进行相应的修改。

相关内容

热门资讯

一分钟秒懂!wepoker辅助... 一分钟秒懂!wepoker辅助器有哪些功能,wepoker免费脚本咨询,玩家教你(有挂攻略)准备好在...
玩家必看科普!wepoker轻... 玩家必看科普!wepoker轻量版透视方法,拱趴大菠萝自动计算机器人,新2025教程(有挂攻略)准备...
科普常识!佛手大菠萝有挂吗,w... 科普常识!佛手大菠萝有挂吗,wepoker辅助透视,分享教程(有挂透视);佛手大菠萝有挂吗软件透明挂...
推荐十款!wepoker软件安... 推荐十款!wepoker软件安装包,wepokerplus辅助,可靠教程(有挂透明);支持多人共享记...
关于!wejoker辅助器怎么... 关于!wejoker辅助器怎么卖,hhpoker是内部控制吗,2025新版技巧(有挂透视)1、很好的...
技术分享!wepoker私人局... 这是一款非常优秀的wepoker私人局透视插件 ia辅助检测软件,能够让你了解到wepoker私人局...
指导大家!pokemmo手机辅... 指导大家!pokemmo手机辅助软件,hhpoker有没有作弊辅助,系统教程(有挂方法)1、很好的工...
科技通报!拱趴大菠萝万能挂,w... 科技通报!拱趴大菠萝万能挂,wepoker-h5下载,AI教程(有挂技巧)1、点击下载安装,微扑克w...
玩家攻略!wepoker辅助器... 1、玩家攻略!wepoker辅助器安装包,wepoker俱乐部辅助器,细节揭秘(有挂攻略)(UU p...
一分钟揭秘!大菠萝免费辅助,w... 一分钟揭秘!大菠萝免费辅助,wepoker免费透视,新版2025教程(有挂教程);支持2-10人实时...