简体   繁体   English

.subscribe 中的 router.navigate() 停止应用程序的功能

[英]router.navigate() inside .subscribe halts functionality of the application

I am trying to signin a user to my application and upon successful signin the user will get redirected to the home page ( / ).我正在尝试将用户登录到我的应用程序,成功登录后,用户将被重定向到主页 ( / )。

Everything works fine here where the user gets redirected to the home page, but I am not able to navigate by clicking any links that are on the home page.这里一切正常,用户被重定向到主页,但我无法通过单击主页上的任何链接进行导航。

However, after examining the issue I noticed that, when I move the this.router.navigate(['/'], { relativeTo: this.route }) out of the subscribe block in the login method (given below) the page gets redirected to the home page and all functionality seems to be working fine.但是,在检查了这个问题之后,我注意到,当我将this.router.navigate(['/'], { relativeTo: this.route })移出login方法(如下所示)的subscribe块时,页面得到重定向到主页,所有功能似乎都运行良好。

There is some issue with the way I am calling the this.router.navigate from the subscribe block.我从subscribe块调用this.router.navigate的方式存在一些问题。 Can someone help me out discovering what is going on?有人可以帮我发现发生了什么吗? Thanks.谢谢。

Note: I also found a similar question on SO .注意:我也在SO 上发现了一个类似的问题 The suggested answer to assign router locally did not work.在本地分配router的建议答案不起作用。 Angular version - 11角度版本 - 11

login(pwd: any){
    this.loginService.login(usrname,pwd).subscribe(
    (response) =>
     {
          console.log("success executed");

          this.router.navigate(['/'], { relativeTo: this.route }).then(x => console.log("????? "+x));
        return;
    },
    (error) => 
    {
         console.log("ERRR  == "+JSON.stringify(error));

    }
   );

}

UPDATE更新

APP ROUTING MODULE.应用路由模块。

  { path: '', loadChildren: () => import('./home-page/home-page.module').then(m => m.HomePageModule) , pathMatch:"full"},

  {
    path: 'signup',
    loadChildren: () => import('./Reg/Reg.module').then(m => m.RegModule) 

  },

RegRoutingModule路由模块

import { SignUpComponent } from './sign-up/sign-up.component';

const routes: Routes = [
  { path: '', component: SignUpComponent },
  { path: 'signin', component: SignInComponent },

];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class RegRoutingModule { }

When User decides to signin to the application, they go into the Signup page ( SignUpComponent ), and then click on the link SIGN IN ( SignInComponent ).当用户决定登录应用程序时,他们会进入注册页面 ( SignUpComponent ),然后单击链接登录 ( SignInComponent )。 This is a lazy loaded module.这是一个延迟加载的模块。

The Homepage component is also a lazy loaded module. Homepage 组件也是一个延迟加载的模块。

UPDATE 2更新 2

HomePageRoutingModule主页路由模块

import { HomePageComponent } from './home-page.component';


const routes: Routes = [{ path: '', component: HomePageComponent },
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class HomePageRoutingModule { }

Most likely it's a problem of routes configuration, or even of some route guards that act in an unexpected way.很可能是路由配置的问题,或者甚至是某些以意外方式运行的路由守卫的问题。

Are you using eager or lazy loading for your HomeComponent ?您是为HomeComponent使用急切加载还是延迟加载? Showing the module where you configure your routes would be helpful, and I bet that the problem lies in there.显示配置路由的模块会很有帮助,我敢打赌问题出在那里。


Also, I'm confused why you need to use { relativeTo: this.route } when you do the navigation.另外,我很困惑为什么在导航时需要使用{ relativeTo: this.route } Ideally you wouldn't need any route scoping at this point.理想情况下,此时您不需要任何路由范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM