Guest User

Untitled

a guest
Apr 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. @Component({
  2. selector : 'app-state',
  3. template: `
  4. <p>Here's the state: {{state}}</p>
  5. `
  6. })
  7. export class StateComponent {
  8. state: string = "My state.";
  9. }
  10.  
  11. @Component({
  12. selector : 'app-base',
  13. template: `
  14. <div>
  15. <p>BaseComponent</p>
  16. <app-state></app-state>
  17. </div>
  18. `
  19. })
  20. export class BaseComponent {
  21. @ViewChild(StateComponent) stateCmp: StateComponent;
  22.  
  23. getState(): string {
  24. return this.stateCmp.state;
  25. }
  26. }
  27.  
  28. @Component({
  29. selector : 'app-inherited',
  30. template: `
  31. <app-base></app-base>
  32. <div>
  33. <p>InheritedComponent</p>
  34. </div>
  35. `
  36. })
  37. export class InheritedComponent extends BaseComponent {
  38. constructor() {
  39. super();
  40. }
  41. }
  42.  
  43. @Component({
  44. selector: 'app-root',
  45. template: `
  46. <app-inherited></app-inherited>
  47. <button (click)="onClick()">Get state</button>
  48. <p>{{ theState }}</p>
  49. `
  50. })
  51. export class AppComponent {
  52. @ViewChild(InheritedComponent) cmpRef: InheritedComponent;
  53.  
  54. theState: string;
  55.  
  56. onClick() {
  57. this.theState = this.cmpRef.getState();
  58. }
  59. }
Add Comment
Please, Sign In to add comment