Reputation: 2663
I want to share data in angular between two components. I did as explained here and here. Below is my code
login.component.ts
import { Component, OnInit } from '@angular/core';
import { User } from '../domain/User';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { LoginResponse } from '../domain/LoginResponse'
import { DataSharingService } from '../service/DataSharingService';
import { Router } from '@angular/router';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css'],
providers: [DataSharingService]
})
export class LoginComponent implements OnInit {
private user:User = new User();
private xSSOFamilyId:string = 'BOMO';
constructor(private httpClient: HttpClient, private dataSharingService: DataSharingService, private router: Router) { }
ngOnInit() {
}
login(): void {
var authorization:string = 'Basic ' + btoa(this.user.cwopa + ":" + this.user.password);
var httpHeaders = new HttpHeaders({
'Authorization': authorization,
'userId': this.user.cwopa,
'X-SSO-Family-Id': this.xSSOFamilyId
});
this.httpClient.post<LoginResponse>('http://localhost:8081/web-beginner/authenticate', undefined, {headers: httpHeaders}).subscribe(response => {
this.router.navigate(['dashboard'])
}, error => {
console.log("error occured");
});
}
public getApiKey() {
this.dataSharingService.setData({apikey: '1234'});
}
}
dashboard.component.ts
import { Component, OnInit } from '@angular/core';
import { DataSharingService } from '../service/DataSharingService';
import { LoginComponent } from '../login/login.component';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css'],
providers: [DataSharingService, LoginComponent]
})
export class DashboardComponent implements OnInit {
constructor(private dataSharingService: DataSharingService, private loginComponent: LoginComponent) { }
ngOnInit() {
this.dataSharingService.currentData.subscribe(data => {
console.log(data)
});
}
authorize(appName:string): void {
console.log(this.loginComponent.getApiKey())
}
}
I have a link in dashboard.component.html. Each time I click it, onNgInit and authorize functions are called.
Furthermore, the data I want to receive in dashboard.component.ts's authorize function is print undefined and onNgInit is printing the expected value.
Can anyone please tell me whats wrong with the code?
Upvotes: 0
Views: 105
Reputation: 1648
remove providers: [DataSharingService] from login component add DataSharingService to the app.module.ts provider so it will be singleton.
Don't inject component as this through constructor.
private loginComponent: LoginComponent
Just use a service to share data or if your DashboardComponent.html contain LoginComponent as a component, you can use @ViewChild As Sujay's answer. So you can use same DataSharingService for getting api key.
Upvotes: 2
Reputation: 653
You need to get the reference of the LoginComponent
using @ViewChild
keyword
import { Component, OnInit } from '@angular/core';
import { DataSharingService } from '../service/DataSharingService';
import { LoginComponent } from '../login/login.component';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css'],
providers: [DataSharingService, LoginComponent]
})
export class DashboardComponent implements OnInit {
@ViewChild(LoginComponent) loginComponent: LoginComponent
constructor(private dataSharingService: DataSharingService) { }
ngOnInit() {
this.dataSharingService.currentData.subscribe(data => {
console.log(data)
});
}
authorize(appName:string): void {
console.log(this.loginComponent.getApiKey())
}
}
Upvotes: 0