Accessing a property in a parent Component

后端 未结 6 714
春和景丽
春和景丽 2020-11-29 23:41

I have a property in a top level Component that is used data from a HTTP source like so (this is in a file called app.ts):

import {UserData} fro         


        
6条回答
  •  被撕碎了的回忆
    2020-11-30 00:20

    You could:

    • Define a userStatus parameter for the child component and provide the value when using this component from the parent:

      @Component({
        (...)
      })
      export class Profile implements OnInit {
        @Input()
        userStatus:UserStatus;
      
        (...)
      }
      

      and in the parent:

      
      
    • Inject the parent into the child component:

      @Component({
        (...)
      })
      export class Profile implements OnInit {
        constructor(app:App) {
          this.userStatus = app.userStatus;
        }
      
        (...)
      }
      

      Be careful about cyclic dependencies between them.

提交回复
热议问题