3 回答

TA貢獻1880條經(jīng)驗 獲得超4個贊
許多解決方案之一是創(chuàng)建一個@Injectable()類,其中包含要在標(biāo)題中顯示的數(shù)據(jù)。其他組件也可以訪問此類并更改此數(shù)據(jù),從而有效地更改標(biāo)題。
另一個選擇是設(shè)置@Input()變量和@Output()EventEmitters,可用于更改標(biāo)頭數(shù)據(jù)。
根據(jù)需要編輯示例:
@Injectable()
export class HeaderService {
private _data;
set data(value) {
this._data = value;
}
get data() {
return this._data;
}
}
在其他組件中:
constructor(private headerService: HeaderService) {}
// Somewhere
this.headerService.data = 'abc';
在標(biāo)題組件中:
let headerData;
constructor(private headerService: HeaderService) {
this.headerData = this.headerService.data;
}
我實際上還沒有嘗試過。如果獲取/設(shè)置無效,則可以將其更改為使用Subject();。
// Simple Subject() example:
let subject = new Subject();
this.subject.subscribe(response => {
console.log(response); // Logs 'hello'
});
this.subject.next('hello');
添加回答
舉報