在角度8中将没有父子关系的一个组件的数据绑定到另一组件

我有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)
      }
    )
  }

}
X00400 回答:在角度8中将没有父子关系的一个组件的数据绑定到另一组件

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3166392.html

大家都在问