【发布时间】:2019-06-15 09:10:28
【问题描述】:
所以我最近才开始进行 Angular 开发,但我完全缺少一些东西。我有一个使用app.component.html 设置的基本应用程序,如下所示:
<h1>{{ routeTitle }}</h1>
<router-outlet></router-outlet>
app.component.ts 设置为:
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
@Input() routeTitle;
}
然后我设置了 2 个其他基本组件,只是为了表明我的路由有效(它们确实如此),但例如在 dashboard.component.ts 上,我似乎无法从它(作为子组件)传递 routeTitle直到父(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)';
}
}
}
请帮忙,因为这让我发疯了,为什么我似乎无法理解一些不应该花这么长时间才能弄清楚的事情。谢谢!
【问题讨论】:
-
查看this,了解使用
@Output()的正确方法。 -
如果它是动态属性,那么您可以在您的父级中使用订阅该属性。您可以使用主题来做到这一点。
标签: angular angular-routing pass-data