角默认的路线总是预先加载

0

的问题

我有两条路:

  • 家["]
  • 关于['关于']

当我去到关于直接与我的浏览器,我已经通知的2件事:

  • 我的家庭模块预载的(而我已经请求的/有关)
  • 该网址是rewritted这样的:/about->/->/约

应路由。模块。t

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [
  { path: '', loadChildren: () => import('./routes/home/home.module').then(m => m.RoutesHomeModule) },
  { path: 'about', loadChildren: () => import('./routes/about/about.module').then(m => m.RoutesAboutModule) },
  { path: '**', redirectTo: '', pathMatch: 'full' }
];

@NgModule({
  imports: [ RouterModule.forRoot(routes, { relativeLinkResolution: 'legacy' }) ],
  exports: [ RouterModule ]
})
export class AppRoutingModule { }

应用程序。组成部分。ts

import { Component, OnInit } from '@angular/core';
import { NavigationStart, Router } from '@angular/router';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.less'],
})
export class AppComponent implements OnInit {

  constructor(
    private router: Router,
  ) { }

  ngOnInit(): void {
    this.router.events.subscribe(event => {
      if (event instanceof NavigationStart) {
        // outputs "/about" but NEVER "/"
        console.log(event.url);
      }
    });
  }

}

角:13.0.2
铬:96.0.4664.55
操作系统:mac os12.0.1

angular lazy-loading
2021-11-24 05:42:28
1

最好的答案

0

跳过预的家庭模块可以更新的路线阵列如下。

const routes: Routes = [
  { path: 'home', loadChildren: () => import('./routes/home/home.module').then(m => m.RoutesHomeModule)},
  { path: 'about', loadChildren: () => import('./routes/about/about.module').then(m => m.RoutesAboutModule) },
  { path: '**', redirectTo: '', pathMatch: 'full' }
];

这可能会限制预压.

你可以指定为 path: 'home' 而不是的 path: ''.

2021-11-24 12:54:52

有什么变化,你呢?
Ziad

更新我的回答@齐亚德
Jai Saravanan

家庭模块未预装了,但我仍然有URL rewritting:/about->/->/约
Ziad

所以当你得到 /about 这是重新定向为`/`?
Jai Saravanan

是的然后它可以追溯到关
Ziad

检查你的代码组件或身份验证。警卫,任何重新定向的逻辑了。 你的路线似乎是不错的。
Jai Saravanan

是它的工作@齐亚德?
Jai Saravanan

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................