Angular中的嵌套@input属性
创始人
2024-10-31 08:31:17
0

假设有一个父组件 ParentComponent 和一个子组件 ChildComponent。ChildComponent 根据 ParentComponent 传递给它的属性渲染不同的内容。ChildComponent 还有一个子组件 GrandchildComponent,因此,ParentComponent 需要将某些属性传递给 GrandchildComponent。

下面是处理这个问题的步骤:

  1. 在 ParentComponent 中创建一个对象属性,该对象属性包含 ParentComponent 需要传递到 ChildComponent 的所有属性。

parent.component.ts:

export class ParentComponent {
  grandchildInputs = { name: 'Grandchild', age: 12 };
}
  1. 在 ChildComponent 中添加 @Input() 装饰器,用于接收从 ParentComponent 传递而来的属性,并将这些属性传递给 GrandchildComponent。

child.component.ts:

import { Component, Input } from '@angular/core';

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

{{name}}

Age: {{age}}

` }) export class ChildComponent { @Input() name: string; @Input() age: number; @Input() grandchildInputs: any; }
  1. 在 GrandchildComponent 中添加 @Input() 装饰器,用于接收从 ParentComponent 传递而来的属性。

grandchild.component.ts:

import { Component, Input } from '@angular/core';

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

{{name}}

Age: {{age}}

{{desc}}

` }) export class GrandchildComponent { @Input() name: string; @Input() age: number; @Input() inputs: any; get desc() { return `${this.name} is ${this.age} years old and ${this.inputs.hobby}`; } }
  1. 在 ParentComponent 中使用 ChildComponent,并传递需要的属性。

parent.component.html:


现在 ParentComponent 将一个对象传递给了 ChildComponent,然后 ChildComponent 又将同样的对象传递给 GrandchildComponent。

这是嵌套 @Input 属性的一种解决方法,通过将属性存储

相关内容

热门资讯

于此同时"aapok... 于此同时"aapoker怎么设置抽水"wpk辅助(一直真的是有辅助工具)-哔哩哔哩aapoker怎么...
透视透视"werpl... 透视透视"werplan脚本"hhpoker哪个俱乐部靠谱(果然真的有辅助插件)-哔哩哔哩运hhpo...
透视好牌"wepok... 透视好牌"wepoker透视有没有"wepoker私人局开挂视频(好像是有辅助平台)-哔哩哔哩1、w...
围绕透视问题"哈糖大... 围绕透视问题"哈糖大菠萝破解器"aapoker辅助器是真的吗(本来真的有辅助下载)-哔哩哔哩进入游戏...
今天下午"wepok... 您好,德州辅助工具到底怎么样这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】...
日前"wepoker... 日前"wepoker脚本下载"wepoker私局代打(竟然真的有辅助辅助器)-哔哩哔哩1、每一步都需...
据统计"pokern... 您好,we-poker有人玩吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054...
透视讲解"wpk作必... 透视讲解"wpk作必弊是真的吗"wepoker透视app下载(真是真的是有辅助平台)-哔哩哔哩1、完...
透视能赢"aapok... 透视能赢"aapoker辅助插件工具"wpk俱乐部有没有辅助(其实是真的辅助安装)-哔哩哔哩1、这是...
现有说明如下"wep... 现有说明如下"wepoker俱乐部辅助"德普之星透视辅助插件(总是存在有辅助辅助器)-哔哩哔哩1、德...