Angular 7-从父组件访问子组件的动态属性

Angular 7-从父组件访问子组件的动态属性,angular,angular-routing,pass-data,Angular,Angular Routing,Pass Data,所以我最近开始了角度的发展,有一点我完全忽略了。我用app.component.html设置了一个基本应用程序,如下所示: <h1>{{ routeTitle }}</h1> <router-outlet></router-outlet> @Component({ selector: 'my-child-component', templateUrl: './myChild.component.html' }) export class

所以我最近开始了角度的发展,有一点我完全忽略了。我用
app.component.html
设置了一个基本应用程序,如下所示:

<h1>{{ routeTitle }}</h1>
<router-outlet></router-outlet>
@Component({
  selector: 'my-child-component',
  templateUrl: './myChild.component.html'
})
export class MyChildComponent {
  @Input() myInputProperty;
  // ...
}
然后,我设置了另外两个基本组件,以显示我的路由工作(它们确实如此),但例如在
仪表板.component.ts
上,我似乎无法将
路由文件
从它(作为子组件)传递回父组件(app.component)以显示在H1标记中:

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

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.scss']
})
export class DashboardComponent implements OnInit {
  @Output() routeTitle;

  constructor() { }

  ngOnInit() {
    if (...somelogic...) {
      this.routeTitle = 'Dashboard';
    }
    else {
      this.routeTitle = 'Dashboard (Courier)';
    }
  }
}
请帮帮我,因为这让我发疯了,为什么我不能把我的头放在不应该花这么长时间去弄清楚的事情上。谢谢大家!

单词“child”用得太多了,可能会有点混淆

此代码:

<h1>{{ routeTitle }}</h1>
<router-outlet></router-outlet>
其中子组件定义如下:

<h1>{{ routeTitle }}</h1>
<router-outlet></router-outlet>
@Component({
  selector: 'my-child-component',
  templateUrl: './myChild.component.html'
})
export class MyChildComponent {
  @Input() myInputProperty;
  // ...
}
我假设您真正想做的是在路由之间传递数据?如果是这样,您根本不想使用
输入
/
输出
属性。相反,您希望使用多种技术中的一种在路由之间传递数据


此外,Angular v7.2还增加了另一项技术:

有两种情况需要在组件之间传递数据:

  • 如果您想将一些数据从子级传递到父级,那么应该使用@Output

  • 有时我们需要在同级组件之间传递数据,在那里我们可以使用共享服务并利用行为主体来传递数据

    现在使用Angular7.2,您可以使用


  • 您也可以查看以下内容。

    有关使用
    @Output()
    的正确方法,请参阅。如果是动态属性,则可以在父级中使用“订阅该属性”。你可以用一个主题来做这件事。谢谢Deboarh的深入解释!我最终阅读了Angular Services,并使用
    BehaviorSubject
    实现了一个解决方案。稍后我会在下面发布我的最终解决方案