Cross-Component communication on a Single Page Application in Angular 8

蹲街弑〆低调 提交于 2019-12-11 10:57:32

问题


Am working on a Single Page Application using Angular 8 on the frontend and Laravel on the backend. After capturing the data from a login form then submitting to the backend via JWT which works fine. Later I get a response on the logic file of the form (login.component.ts) from the backend.

Now am trying to pass the response from the login component to another component via a shared service called AuthCheck.Finally I pass the data to the Navbar Component file and finally display it on the view. Basically cross-component communication.

In the service am using rxjs for listening to the response and setting an observable.Next am trying to assign my data to a variable in the service which I listen to in the Navbar component logic file and display on the navbar view..

The problem is that I keep getting an error of ERROR TypeError: "this.AuthCheck.checkUser is not a function" which is thrown from the login component

~ Kindly assist?

Login.component.ts

import { Component, OnInit } from '@angular/core';
import { AuthService } from 'src/app/Services/auth.service';
import { TokenService } from 'src/app/Services/token.service';
import { Router } from '@angular/router';
import { AuthCheckService } from 'src/app/Services/auth-check.service';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  public form = {
    email: null,
    password: null
  };

  public error = null;

  constructor(
    private Auth:AuthService,
    private Token:TokenService,
    private router: Router,
    private AuthCheck : AuthCheckService
  ) { }

  //Submit the form data to the backend
  onSubmit(){
    this.Auth.login(this.form).subscribe(
      data => this.handleResponse(data),
      error => this.handleError(error)
    );
  }

  //Function to listen to the response
  handleResponse(data){
    //Pass the data to checkUser method in AuthCheckService
    this.AuthCheck.checkUser(data.user);
  }

  //Handles any errors
  handleError(error){
      this.error = error.error.error;
  }

  ngOnInit() {
  }

}

AuthCheck Service

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class AuthCheckService {

private userName = null;

    private userName = new BehaviorSubject<string>(this.checkUser());
    checkUser$ = this.userName.asObservable();

    checkUser(data: any){
        this.user = data;
        this.userName.next(this.user);
    }

    constructor() { }
}

Navbar Component ts file

import { Component, OnInit } from '@angular/core';
import { AuthCheckService } from 'src/app/Services/auth-check.service';

@Component({
  selector: 'app-navbar',
  templateUrl: './navbar.component.html',
  styleUrls: ['./navbar.component.css']
})
export class NavbarComponent implements OnInit {

  public userName : string;

  constructor(
    private AuthCheck : AuthCheckService
  ) { }

  ngOnInit() {
      this.AuthCheck.checkUser$.subscribe(message => this.userName = message);
  }
}

Navbar Component (Markup file)

<a class="navbar-brand" *ngIf="loggedIn">Welcome {{ userName }}</a>

回答1:


You created a property and a method with the same name (checkUser) change it as follows:

change the checkUser to checkUser$. You should also set the BehaviorSubject to the userName. Please uncomment the line

AuthCheck Service

//private userName = null;

//Check logged in userName
private userName = new BehaviorSubject<string>('Martin');
checkUser$ = this.userName.asObservable();


checkUser(data: any){
    this.user = data;
    this.userName.next(this.user);
}

In your navbar component

ngOnInit() {
   this.AuthCheck.checkUser$.subscribe(message => this.userName = message);
}


来源:https://stackoverflow.com/questions/56845886/cross-component-communication-on-a-single-page-application-in-angular-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!