代码之家  ›  专栏  ›  技术社区  ›  Hydroper

如何定义角度组件中的参数?

  •  0
  • Hydroper  · 技术社区  · 1 年前

    我有一个组件 modal 它有一个 darkUnderneath 所有物我正在尝试使用绑定 [darkUnderneath]="true" 在我的父组件的模板中,并且得到:

    无法绑定到“darkUnderneath”,因为它不是“modal”的已知属性。

    我需要使用 @Output 室内装修设计师我试着定义 深色下面 带和不带 @输出 来自的装饰器 @angular/core

    <modal [darkUnderneath]="blah"></modal>
    
    export class ModalComponent implements OnInit {
      @Output()
      darkUnderneath: boolean = false;
      // ...
    }
    
    2 回复  |  直到 1 年前
        1
  •  2
  •   UncleDave    1 年前

    你正在寻找 @Input 室内装修设计师

    export class ModalComponent implements OnInit {
      @Input()
      darkUnderneath: boolean = false;
      // ...
    }
    
        2
  •  2
  •   Manoj Prasanna    1 年前

    在Angular中,当您想将数据从子组件传递给父组件时,需要使用@Output()装饰器和EventEmitter。然而,你的问题似乎有点混乱。如果要将数据从父组件传递到子组件,可以使用@Input()装饰器,而不是@Output()。

    为了澄清这两种情况,以下是使用@Input()和@Output()装饰器在子组件和父组件之间传递数据的正确方法:

    将数据从父级传递给子级(使用@Input()): 父组件(Parent.Component.ts):

    从“@angular/core”导入{Component};

    @Component({
      selector: 'app-parent',
      template: '<app-child [dataFromParent]="data"></app-child>',
    })
    export class ParentComponent {
      data: string = "Data from Parent";
    }
    

    子组件(Child.Component.ts):

    import { Component, Input } from '@angular/core';
    
    @Component({
      selector: 'app-child',
      template: '<p>{{ dataFromParent }}</p>',
    })
    export class ChildComponent {
      @Input() dataFromParent: string;
    }
    

    我希望它能帮助你清楚地理解