我试图在登录成功后在标题导航栏中显示已登录的用户,但出于某种原因,它拒绝出现在导航栏中。
app.component.ts(as first component) -> db-srv-conn.service.ts(as service) -> main.component.ts(as destination component)db-srv-con.service.ts :
import { HttpClient } from '@angular/common/http';
import { BehaviorSubject, Observable } from 'rxjs';
export class DbSrvConnService {
private bsubject=new BehaviorSubject<any>("t");
constructor(private http:HttpClient) { }
sendMassage(massage: string) {
this.bsubject.next(massage);
}
getMassage(): Observable<any> {
return this.subject.asObservable();
}
}app.component.ts(发送方组件):
import { DbSrvConnService } from 'src/app/sevices/db-srv-conn.service';
export class AppComponent{
constructor(private DbSrvConnService:DbSrvConnService) { }
...
login(){
const massage=this.formGroupLogin.value.massage;
if (status==1){
this.DbSrvConnService.send_user(massage);
}
}main.component.ts(循环组件):
import { DbSrvConnService } from 'src/app/sevices/db-srv-conn.service';
...
massages: any;
subscription: Subscription;
constructor(private DbSrvConnService:DbSrvConnService) {
this.subscription = this.DbSrvConnService.getMessage().subscribe(massage => {
console.log(massage);
});但是,没有价值的按摩,请帮助我。
发布于 2021-08-16 11:10:15
尝试这些步骤,我认为打字是导致这个问题的原因。
return this.subject.asObservable();替换为getMassage(): Observable<any> {
return this.bsubject.asObservable();
}this.DbSrvConnService.send_user(massage);替换为以下login(){
const massage=this.formGroupLogin.value.massage;
if (status==1){
this.DbSrvConnService.sendMassage(massage);
}
// navigation or whatever down here
}https://stackoverflow.com/questions/68799324
复制相似问题