Angular:Router.navige不在某些页面上加载组件

发布于 2025-01-21 03:51:20 字数 1988 浏览 0 评论 0原文

我将Angular从10更新到12,在迁移后,我的网站的某些页面不再使用Router.navigate.navigate(或routerlink)加载组件。尽管URL更改并正确(如果我刷新,则加载了正确的内容)。据我了解,这可能来自路由表,但是我并没有真正看到这将如何解释它,因为它在Angular 10中起作用。

Shell.childRoutes([
    {
      path: '',
      component: LoginComponent,
      canActivate: [RedirectionGuardService],
      pathMatch: 'full'
    },
    {
      path: 'component1',
      component: componentComponent1,
      data: { title: marker('Component1'), data: { roles: ['Role.USER'] } },
    },
    {
      path: 'component2/:query',
      component: component2Component,
      data: { title: marker('component2') },
      resolve: { data: component2Resolver },
    },
    {
      path: 'component3/:id/:value',
      component: component3Component,
      resolve: { data: component3Resolver },
      data: { title: marker('component3') },
    },
    {
      path: 'component4/:id',
      component: component4Component,
      resolve: { data: component4Resolver },
      data: { title: marker('component4') },
    },
    {
      path: 'component5/:id',
      component: component5Component,
      resolve: { data: component3Resolver },
      data: { title: marker('component5') },
    },
  ]),
  {
path: 'waitingComponent',
component: waitingComponent,
data: { title: marker('Waiting') },
  },
  {
path: 'uploadModal/:id/:value',
component: UploadModalComponent,
data: { title: marker('Upload Content') },
  },
  {
path: 'faq',
component: FAQComponent,
  },

在我提供的代码中,router.navigatecomponent2uploadcomponent中不起作用,但在component1component3

是关于如何编写router.navigate的问题:

this.router.navigate(['/component3/',this.value1, this.value2]);

如果这

我还尝试使用固定值尝试,但不会改变情况。 URL会更改,但没有变化。

在编译或使用浏览器(控制台或网络)的过程中,我不会收到任何错误。

任何帮助都是有价值的。

I updated Angular from 10 to 12, and after the migration some pages of my website no longer load the components with a router.navigate (or a routerLink for that matter), although the URL is changed and correct (if I refresh, the right content is loaded). From what I understand this could come from the routing-table, but I don't really see how that would explain it since it worked back in angular 10.

Shell.childRoutes([
    {
      path: '',
      component: LoginComponent,
      canActivate: [RedirectionGuardService],
      pathMatch: 'full'
    },
    {
      path: 'component1',
      component: componentComponent1,
      data: { title: marker('Component1'), data: { roles: ['Role.USER'] } },
    },
    {
      path: 'component2/:query',
      component: component2Component,
      data: { title: marker('component2') },
      resolve: { data: component2Resolver },
    },
    {
      path: 'component3/:id/:value',
      component: component3Component,
      resolve: { data: component3Resolver },
      data: { title: marker('component3') },
    },
    {
      path: 'component4/:id',
      component: component4Component,
      resolve: { data: component4Resolver },
      data: { title: marker('component4') },
    },
    {
      path: 'component5/:id',
      component: component5Component,
      resolve: { data: component3Resolver },
      data: { title: marker('component5') },
    },
  ]),
  {
path: 'waitingComponent',
component: waitingComponent,
data: { title: marker('Waiting') },
  },
  {
path: 'uploadModal/:id/:value',
component: UploadModalComponent,
data: { title: marker('Upload Content') },
  },
  {
path: 'faq',
component: FAQComponent,
  },

In the code I provided, router.navigate doesn't work in component2 or uploadComponent but works in component1 or component3.

If it's a matter of how the router.navigate is written:

this.router.navigate(['/component3/',this.value1, this.value2]);

(This is used by the UploadComponent as an example).

I also tried it with a fixed value to try, and it doesn't change the situation. The URL changes, but not the component.

I don't receive any errors during compiling or with the browser (console or network).

Any help would be of value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心清如水 2025-01-28 03:51:20

好吧,我找到了引起问题的原因,花了我一段时间,但是这里是。

Routereusestrategy的配置中,参数shore reuseroute是错误配置的,在Angular 10和11之间的更改中,发生了变化,并且以前的配置不再符合Angular的标准。

配置应该看起来像这样:

返回Future.RouteConfig === Current.RouteConfig;

希望这将帮助那些在从Angular 10迁移到Angular 11期间也遇到此问题的人。

Okay I found what was causing the problem, took me a while but here it is.

In the configuration of RouteReuseStrategy the parameter shouldReuseRoute was wrongly configured, during the change between Angular 10 and 11 something changed and the former configuration was no longer up to the standards of Angular.

The configuration should look something like this :

return future.routeConfig === current.routeConfig;

Hopefully this will help people who also had this problem during a migration from Angular 10 to Angular 11.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文