각 단계를 수행 한 후에도 동일한 문제가 발생합니다. 아직Ng2 서비스를 통한 구성 요소 간 데이터 공유
공유 서비스를 통해 구성 요소간에 데이터를 공유 할 수 없습니다.
내 워크 플로 : 로그인 서비스를 통해 로그인 한 후 정보 페이지에 UserDetails 응답을 공유하려고했습니다.
는 난 단지 == 제공
=== 로그인 구성 요소 =====
import { Component } from '@angular/core';
import { Http } from '@angular/http';
import { UserAccount } from '../model/userAccount.interface';
import { LoginService } from './login.service';
import { Router } from '@angular/router';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
})
export class LoginComponent {
emailAddress : string;
password : string;
submitted : boolean;
errorMessage : string;
constructor(private loginService: LoginService, private router : Router) {
this.submitted = false;
}
login() {
// event.preventDefault();
this.submitted = true;
this.loginService.getLogin(this.emailAddress, this.password).subscribe(
u => this.router.navigate(['/about']),
error => this.errorMessage = <any>error);
}
}
로 @NgModule에 app.module.ts에 로그인 서비스를 주입 한 구성 요소 ===== 소개 = 로그인 서비스 ====@Injectable()
export class LoginService {
private userAccount : UserAccount[];
constructor (private http: Http) {}
getLogin(): Observable<UserAccount[]> {
return this.http.get(this.url)
.map(this.extractData);
}
private extractData(res: Response) {
let body = res.json();
this.userAccount = body.data.user[0]
return this.userAccount || { };
}
getUserDetails() {
return this.userAccount;
}
}
======
export class AboutComponent implements OnInit{
// initialize a private variable _data, it's a BehaviorSubject
// private _data = new BehaviorSubject<UserAccount[]>([]);
userDetails : UserAccount[];
lService : LoginService;
constructor(loginService: LoginService) {
this.lService = loginService;
this.userDetails = this.lService.getUserDetails();
console.log(this.userDetails);
}
ngOnInit() {
}
}
전체 구성 요소 코드를 게시 해주십시오. –
은 로그인 구성 요소를 업데이트했습니다. 감사합니다 사전에 – siddh
@siddh'getUserDetails()'무엇입니까? – echonax