我有Home服务,该服务正在使用httpClient调用用户的详细信息API,然后将其导入到Home组件中,并在Home Component中订阅了该服务,这为我提供了该用户的详细信息,即firstname
。并且我已经将该名字存储在变量“名字”中
现在我有另一个组件header.component.html,我想显示firstname
从原始组件到该头组件
主页组件属于仪表板模块,标头组件位于应用程序模块中,并且这些组件之间没有直接或父子关系
帮我了解如何将home组件的firstname
绑定到标头组件
下面是home.component.ts的代码,其中我将firstname
的值存储到变量中以绑定到标头组件
import { Component,OnInit } from '@angular/core';
import { HomeService } from '../../_service/home.service'
@Component({
selector: 'app-home',templateUrl: './home.component.html',styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
users : []
firstname ;
lastname ;
constructor(private homeservice: HomeService) { }
ngOnInit() {
this.homeservice.getuser()
.subscribe(
res =>{
this.users= res.userDetails;
this.firstname = this.users['first_name'];
this.lastname = this.users['last_name'];
console.log(this.users);
console.log(this.firstname)
}
)
}
}