子コンポーネント間の通信を試みていますが、ドキュメントによって、サービスごとにこれを行う必要があります。サービス情報を取得するときに問題が発生します。私がconsole.log()
を与えれば、私は変数に代入すると、それは後でそれにアクセスし、答えとして未定義を与えるconsigではない。別のAngular2 - 親と子がサービスを介して通信する
import { Component, OnInit } from '@angular/core';
import {Angular2TokenService, ResetPasswordData } from 'angular2-token';
import { ActivatedRoute, Router, Params } from '@angular/router';
import { AuthService } from '../services/auth.service';
@Component({
selector: 'app-forgot-passoword',
templateUrl: './forgot-passoword.component.html',
styleUrls: ['./forgot-passoword.component.scss']
})
export class ForgotPassowordComponent implements OnInit {
_resetPasswordData: ResetPasswordData = <ResetPasswordData>{};
tenancy:string;
error:string;
private _sub:any;
constructor(
private _tokenService: Angular2TokenService,
private _route: ActivatedRoute,
private _router: Router,
private _authService: AuthService
) {}
ngOnInit() {
this._sub = this._route.parent.params.subscribe(
params=> this.tenancy = params['tenancy']
)
}
onSubmit(event){
event.preventDefault();
this.error = '';
this._tokenService.resetPassword({
email: this._resetPasswordData.email,
}).subscribe(
res => {
console.log(this.tenancy);
this._authService.confirmResetPassword("check your email");
this._router.navigate([this.tenancy,'signin'])
},
error => this.error = "Error aconteceu algo"
);
}
}
サービスに情報を渡す
娘クラス。
import { Injectable } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
import { Angular2TokenService } from 'angular2-token';
import { environment } from './../../../environments/environment';
import { Subject } from 'rxjs/Subject'
@Injectable()
export class AuthService {
tenancy: string;
private resetPasswordConfirmed = new Subject<string>();
passwordConfirmed$ = this.resetPasswordConfirmed.asObservable();
constructor(private _tokenService: Angular2TokenService,
private activateRoute: ActivatedRoute,
private _location: Location) { }
init(){
this.tenancy = this._location.path().split('/')[1];
let apiBase:string;
if(environment.name==='mock'){
apiBase = `http://${environment.apiEndPoint}`;
} else {
apiBase = `http://${this.tenancy}.${environment.apiEndPoint}`;
}
this._tokenService.init({
apiBase: apiBase
});
}
confirmResetPassword(mensagem: string) {
this.resetPasswordConfirmed.next(mensagem);
}
}
とサービスのデータを使用する他のクラス。
import { ActivatedRoute, Router } from '@angular/router';
import { Angular2TokenService, SignInData } from 'angular2-token';
import { Component, OnInit } from '@angular/core';
import { AuthService } from '../services/auth.service';
import { Subscription } from 'rxjs/Subscription'
@Component({
selector: 'app-signin',
templateUrl: './signin.component.html',
styleUrls: ['./signin.component.scss']
})
export class SigninComponent implements OnInit {
private _signInData: SignInData = <SignInData>{};
tenancy:string;
error:string;
resetPasswordSucess:string;
_sub: any;
subscription: Subscription;
constructor(
private _tokenService: Angular2TokenService,
private _route: ActivatedRoute,
private _router: Router,
private _authService: AuthService
){
this.subscription= _authService.passwordConfirmed$.subscribe(
mensagem =>{
this.resetPasswordSucess = mensagem;
console.log(mensagem + ' Mensagem');
}
);
}
ngOnInit() {
this._sub = this._route.parent.params.subscribe(
params=> this.tenancy = params['tenancy']
);
}
onSubmit(){
this.error = '';
this._tokenService.signIn(this._signInData)
.subscribe(
res => console.log("DEU CERTO"),
error => console.log(error)
);
}
}
購読のconsole.log(this.resetPasswordSucess)
外、値変数が未定義を実行した場合。
変数に値を割り当て、誰もが購読外で見ることができますか?