Nebular: NbMenuService.onItemClick() calls even if not clicked after the first time

Created on 1 Feb 2019  路  4Comments  路  Source: akveo/nebular

Issue type

  • [x] bug report
  • [ ] feature request
  • [ ] question about the decisions made in the repository

Issue description

Current behavior:
"NbMenuService" component's onItemClick event is called even though I don't click to it, and unsubscribe on destroy.

Expected behavior:
NbMenuService.onItemClick() only called on click.

Steps to reproduce:

I added this to header component in ngx-admin, and on logout route I call auth.logout(). I log in and redirect to dashboard. Then if I click logout, it redirects me to login page, everything works proper until now. But after logging out, if I login and redirect to dashboard again, it will call logout directly.

Related code:

test: Subscription;
ngOnInit() {
  this.test = this.menuService.onItemClick()
    .pipe(
             filter(({ tag }) => tag === 'my-context-menu'),
             map(({ item: { title } }) => title),
          ).subscribe(title => {
            if (title == 'Logout') {
              this.router.navigate(['/auth/logout']);
            }
        }
  );
}

ngOnDestroy() {
  test.unsubscribe();
}

Other information:

Firefox
Windows 10
Angular 6
Nebular 2.0.0-rc.9

important bug components quality

Most helpful comment

Hello! I have been trying to fix this issue when you implement the "onItemClick " event you should "unsubscribe", the real problem of @mehmetalpsumer it's a bad usage of ngOnDestroy method. Check out if the class is implemented in your class like:
export class ExampleComponent implements OnInit, OnDestroy {}
And you should call test.unsubscribe(); as a global variable using this. test.unsubscribe();.

It works for me. Have a nice day

All 4 comments

onItemClick event raises up several times, any updates, fixes, workarounds there? Nebular version is 4.2.1

I am facing the same issue. Any update on this, please?

This problem is related to some error in your templates that can cause your constructor and ngOnInit to be called twice, as stated in this issue and a myriad of causes are stated in this stackoverflow thread.
To pinpoint the problem you can:

  • Check your template component and it's parents to check, add some console.log in your ngOnInit to verify if the problem really is the one I'm talking about.
  • Check other unrelated pages to see if this happens in other places of your code

Hello! I have been trying to fix this issue when you implement the "onItemClick " event you should "unsubscribe", the real problem of @mehmetalpsumer it's a bad usage of ngOnDestroy method. Check out if the class is implemented in your class like:
export class ExampleComponent implements OnInit, OnDestroy {}
And you should call test.unsubscribe(); as a global variable using this. test.unsubscribe();.

It works for me. Have a nice day

Was this page helpful?
0 / 5 - 0 ratings