【问题标题】:Access Parent component properties in Child component's html file在子组件的 html 文件中访问父组件属性
【发布时间】:2019-06-30 19:57:36
【问题描述】:

我想将在 Parent 组件中声明的变量/属性访问到 Child 的视图中。有可能吗?

@Component({...})
export class ParentComponent implements OnInit{
  public example: string;
  constructor(){
    this.example = 'display this';
}}

@Component({templateUrl: './child.component.html',})
export class ChildComponent extends ParentComponent implements OnInit{...}

child.component.html

{{示例}}

【问题讨论】:

  • 您是否扩展父母只是为了让孩子拥有example?如果是,您可以改用@Input 来解决您的问题。

标签: angular angular6 angular7 angular-components


【解决方案1】:

您可以使用@Input 将数据从父组件传递给子组件,以将父组件属性访问到子组件

【讨论】:

    猜你喜欢
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 2019-11-23
    • 2016-05-29
    • 2016-05-17
    • 2019-06-15
    • 1970-01-01
    相关资源
    最近更新 更多