在浏览器中输入有效网址时, Angular 路由会一直重定向到主页

标签 angular angular7 angular-router

我有这个路由配置,它工作得很好,只有一件事让我很烦恼:即使导航应用程序有效,当我转到地址栏并按 Enter 时,即使我不更改 url,它也会将我重定向到/dashboard 和点击进入。

这是我的路线配置:

import { Routes } from '@angular/router';

import { FullComponent } from './layouts/full/full.component';

export const Approutes: Routes = [
  {
    path: '',
    component: FullComponent,
    children: [
      {
        path: 'usuarios',
        loadChildren: './pages/usuarios/usuarios.module#UsuariosModule'
      },
      {
        path: 'autores',
        loadChildren: './pages/autores/autores.module#AutoresModule'
      },
      {
        path: 'conteudos',
        loadChildren: './pages/conteudos/conteudos.module#ConteudosModule'
      },
      {
        path: 'projetos',
        loadChildren: './pages/projetos/projetos.module#ProjetosModule'
      },
      {
        path: 'dashboard',
        loadChildren: './pages/dashboard/dashboard.module#DashboardModule'
      },
    ]
  },
  {
    path: 'login',
    loadChildren: './pages/login/login.module#LoginModule'
  },
  {
    path: '',
    redirectTo: '/dashboard',
    pathMatch: 'full'
  }
];

我注意到的另一件事是,当我按 Enter 并观看我的网络时,我可以看到该页面的解析器被调用,但随后我被重定向到/dashboard。

谢谢大家。

编辑:有趣的是,我试图完全删除仪表板路线并只保留其他路线,出于任何原因(或魔法)我的应用程序仍然试图到达仪表板路线,在一次调试中我发现了类似“延迟路线加载”的东西。

编辑 2:我最终在路由器日志中找到了这个:“导航 ID 1 不等于当前导航 ID 2”然后我一直在努力并决定更改我的路由器模块,而不是返回和 const 并在 AppModule 上调用 RouterModule.forRoot (Approutes)我改成这样:
import { Routes, RouterModule } from '@angular/router';

import { FullComponent } from './layouts/full/full.component';
import { NgModule } from '@angular/core';

const routes: Routes = [
  {
    path: '',
    component: FullComponent,
    children: [
      {
        path: 'dashboard',
        loadChildren: './pages/dashboard/dashboard.module#DashboardModule'
      },
      {
        path: 'usuarios',
        loadChildren: './pages/usuarios/usuarios.module#UsuariosModule'
      },
      {
        path: 'autores',
        loadChildren: './pages/autores/autores.module#AutoresModule'
      },
      {
        path: 'conteudos',
        loadChildren: './pages/conteudos/conteudos.module#ConteudosModule'
      },
      {
        path: 'projetos',
        loadChildren: './pages/projetos/projetos.module#ProjetosModule'
      }
    ]
  },
  {
    path: 'login',
    loadChildren: './pages/login/login.module#LoginModule'
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes, {enableTracing: true})],
  exports: [RouterModule]
})

export class AppRoutingModule { }

但它一开始并没有解决,基于我在上面提到的日志中得到的错误,我想知道我的解决方案是否把事情搞砸了。

在子 route ,我有类似的东西:
const routes: Routes = [
  {
    path: '',
    component: UsuariosIndexComponent,
    resolve: {
      paginacao: UsuariosPageResolver
    }
  }
];

在我的解析器中,我有:
export class UsuariosPageResolver implements Resolve<any> {
  pagina = 1;
  itensPorPagina = 10;
  filtro = '';
  constructor(private usuariosService: UsuariosService) { }

  resolve() {
    return this.usuariosService.paginar(this.pagina, this.itensPorPagina, this.filtro);
  }
}

事实是,当我更改路由并评论解析调用时,它起作用了,但是当我取消注释解析时,它继续工作:)

我会继续关注这个,任何新的结果我都会编辑这个。

Edit3:突发新闻即使我没有实现任何关于 AuthGuard 或拦截器或类似的东西,当我登录时会发生错误,如果我清除站点数据然后它会再次工作。

最佳答案

谁能猜到?

我的问题与路由无关,它是一个控制认证的服务,我错误地在构造函数中设置了错误的条件:
if (loggedUser !== null) ths.router.navigateToUrl('/dashboard');
因此,当我登录并通过菜单导航时,它运行良好,但是当我点击重新加载或手动输入 URL 时,我们得到了“错误”(或功能,哈哈),因为再次调用构造函数,然后发生重定向。

在我的辩护中,这一切都发生了,因为我禁用了所有应用程序安全性以加快测试速度,但在这方面被吸了足够长的时间以至于失去了一些头发。

嗯,谢谢大家。

关于在浏览器中输入有效网址时, Angular 路由会一直重定向到主页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55709068/

相关文章:

angular - 使用对象定义定义 FomGroup

html - 如何为匹配给定值的元素设置 id?

angular - 'stream' 的所有声明必须具有相同的修饰符 api-ai-javascript - Dialogflow

主页路径下的 Angular 路由多个组件

angular - 为什么 Angular 坚持为组件使用自定义前缀?

Angular (8) Universal with firebase cloud functions - 无法匹配任何路由

typescript - 检查输入控件是否在angular2中具有某种类型的验证器

angular - 为什么我的 header 在 angular 7 中没有响应?

angular - createUrlTree 忽略 "useHash: true"RouterModule 配置

Angular2 路由器附加组件而不是替换它