gpt4 book ai didi

angular - 通过服务可观察到的失败在组件之间进行通信

转载 作者:太空狗 更新时间:2023-10-29 18:33:20 26 4
gpt4 key购买 nike

我想创建登录表单,当登录成功时,根组件接收数据然后显示它。我创建了登录服务,根组件可以使用 subscribe() 方法接收通知,但它似乎不起作用。这是我的代码:

//app.component.ts
import { Component } from '@angular/core';
import { Router } from '@angular/router';

import { HttpConstant } from './constants/http.response';
import { LoginService } from './services/login.service';

@Component({
moduleId: module.id,
selector: 'app',
templateUrl: 'app.component.html',
providers: [LoginService]
})

export class AppComponent {
currentUser = 'Anonymous';

constructor(private loginService: LoginService) {
console.log(loginService);
loginService.logInAnnouncement$.subscribe(email => {
console.log(email);
this.currentUser = email;
});
}
}
//login.service.ts
import { Injectable } from '@angular/core';
import { Headers, Http, Response } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import { Observable } from 'rxjs';
import { Subject } from 'rxjs/Subject';

import { Profile } from '../entities/profile';
import { GuardService } from './guard.service';

@Injectable()
export class LoginService {

private headers = new Headers();
private loginUrl = 'api/v1/auth/login';
private email = new Subject<string>();

/**
* Observable string streams
*/
logInAnnouncement$ = this.email.asObservable();

constructor(private http: Http, private guard: GuardService) {
}

login(email, password): Observable<any> {
this.headers.append('Content-Type', 'application/json');
this.headers.append('Accept', 'application/json');
return this.http
.post(this.loginUrl, JSON.stringify({email: email, password: password}), {headers: this.headers})
.map((res:Response) => res.json());
}

announceLogIn(email: string) {
this.email.next(email);
this.logInAnnouncement$.subscribe(email=>{
console.log(email);
});
console.log('OK');
}
}
//login.component.ts
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

import { LoginService } from '../services/login.service';
import { Profile } from '../entities/profile';
import { HttpConstant } from '../constants/http.response';


@Component({
moduleId: module.id,
selector: 'fix-login',
templateUrl: 'login.component.html',
providers: [LoginService, HttpConstant],
})

export class LoginComponent implements OnInit {

profile: Profile;
validatingHandler: ValidateError;
// profile: Profile;

constructor(private loginService: LoginService,
private router: Router) {
this.profile = new Profile();
this.validatingHandler = new ValidateError();
}

ngOnInit(): void {

}

doLogin(event): void {
event.preventDefault();
this.loginService.login(this.profile.email, this.profile.password)
.subscribe((data: Object) => {
if (data.status == 0) {
this.loginService.announceLogIn(this.profile.email);
this.router.navigate(['/']);
}
}, err => {

if (err.status == HttpConstant.HTTP_INVALID_INPUT ) {
this.validatingError(err.json());
}
})

}

private validatingError(error): void {
this.validatingHandler = new ValidateError();
this.validatingHandler._email = error.email;
this.validatingHandler._password = error.password;
}


}

class ValidateError {
_email: string;
_password: string;
}

最佳答案

不要将LoginService 添加到每个组件的providers。这样每个组件都会注入(inject)自己的实例。

改为将它添加到 @NgModule() 的提供者中,这样所有组件都将注入(inject)相同的实例。

关于angular - 通过服务可观察到的失败在组件之间进行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39762899/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com