Ng6 behavSubject problem

now, after making a login, dynamically pass the menu data and generate the menu according to the logger. So I use rxjs to write a global service, where setMenu () is to set the data into the service after logging in, and getMenu () is in sidebar to get the corresponding menu data.

import { Injectable,OnInit } from "@angular/core";
import {BehaviorSubject} from "rxjs";

@Injectable()
export class BehaviorService implements OnInit{
    menuBehavior:any;
    menuData:any;

    constructor() {
        this.menuBehavior=new BehaviorSubject(null);
    }

    ngOnInit(){

    }

    setMenu(menuData){
            console.log("");
            this.menuBehavior.next(menuData);
            console.log(this.menuBehavior);//
    }
    getMenu(){
            return this.menuBehavior;//
    }
}

however, in sidebar, when you subscribe, you do get null . I wonder why
injects menu data when you are already in the stream during setMenu. Why not when you subscribe?

ngOnInit() { 

    this.menuService.getMenu().subscribe((v)=>{
      console.log(v);// null   
      this.data=v;
      this.allData=v
    });
  }
May.27,2021

it is recommended that you check the execution order of this.menuBehavior.next (menuData); and this.menuService.getMenu (). Subscribe ((v) = > {


has nothing to do with the order. The point is, where did you manually setMenu? If not, just initialized data

Menu