簡體   English   中英

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

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

我正在嘗試將用戶登錄到我的應用程序,成功登錄后,用戶將被重定向到主頁 ( / )。

這里一切正常,用戶被重定向到主頁,但我無法通過單擊主頁上的任何鏈接進行導航。

但是,在檢查了這個問題之后,我注意到,當我將this.router.navigate(['/'], { relativeTo: this.route })移出login方法(如下所示)的subscribe塊時,頁面得到重定向到主頁,所有功能似乎都運行良好。

我從subscribe塊調用this.router.navigate的方式存在一些問題。 有人可以幫我發現發生了什么嗎? 謝謝。

注意:我也在SO 上發現了一個類似的問題 在本地分配router的建議答案不起作用。 角度版本 - 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));

    }
   );

}

更新

應用路由模塊。

  { 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) 

  },

路由模塊

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 { }

當用戶決定登錄應用程序時,他們會進入注冊頁面 ( SignUpComponent ),然后單擊鏈接登錄 ( SignInComponent )。 這是一個延遲加載的模塊。

Homepage 組件也是一個延遲加載的模塊。

更新 2

主頁路由模塊

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


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

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

很可能是路由配置的問題,或者甚至是某些以意外方式運行的路由守衛的問題。

您是為HomeComponent使用急切加載還是延遲加載? 顯示配置路由的模塊會很有幫助,我敢打賭問題出在那里。


另外,我很困惑為什么在導航時需要使用{ relativeTo: this.route } 理想情況下,此時您不需要任何路由范圍。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM