未执行对服务的调用

问题描述 投票:-1回答:1

在ngOnInit中,我调用一个服务来获取令牌,但是它不起作用,就像代码的那一部分被省略了,它没有出现在网络请求中,该服务起作用了,因为我在另一个地方使用了它组件,但是我现在需要从那里使用它,该文件是app.component.ts

//imports
export class AppComponent implements OnDestroy {
  title = 'cor4edu-fe';
  menu: NbMenuItem[];
  alive: boolean = true;

  constructor(private sidebarService: NbSidebarService,
              private appMenu: AppMenu, 
              public auth: AuthService) {}

  ngOnInit(){
    this.auth.getTokenSilently$().subscribe((token) => {
      console.log(token);
      localStorage.setItem("Token", token);
    })
    this.myFuncion();
    this.initMenu();
  }

  getToken() {
    return new Promise((resolve) => {
    this.auth.getTokenSilently$().subscribe(token => resolve(token));
    });
    }
    async myFuncion() {
    const token = await this.getToken();
    console.log(token);
    }
  toggle() {
    this.sidebarService.toggle(true);
    return false;
  }
  initMenu() {
    const token= localStorage.getItem("Token");
    const tokenPayload = decode(token);
    const permissions=tokenPayload['permissions'];

    this.appMenu.getMenu()
      .pipe(takeWhile(() => this.alive))
      .subscribe(menu => {
        console.log(localStorage.getItem("Token"));
        const exist = (menu, permissions) => menu.filter(el1=>permissions.some(el2=> "/"+el2.split(":")[1]==el1.link));
        const menun=exist(menu, permissions);

        if(this.auth.isAuthenticated()){
          this.menu = menun;
        }else{
          this.menu=[
            {
              title: 'Dashboard',
              icon: 'home-outline',
              link: '/',
              home: true,
              children: undefined,
            }
          ]
        }
      });
  }
  ngOnDestroy(): void {
    this.alive = false;
  }
}
angular angular-services angular-httpclient
1个回答
1
投票

您的组件未实现OnInit接口,因此不会调用ngOnInit。

© www.soinside.com 2019 - 2024. All rights reserved.