Angular 15 this.router.navigate is not a function, this.router.navigateByUrl is NOT a function

huangapple go评论85阅读模式
英文:

Angular 15 this.router.navigate is not a function, this.router.navigateByUrl is NOT a function

问题

这是一个简短的问题:

以下是我的代码...

  1. import { DOCUMENT } from '@angular/common';
  2. import { Component, HostListener, Inject, NgZone } from '@angular/core';
  3. import { Router } from '@angular/router';
  4. @Component({
  5. selector: 'app-my-dashboards',
  6. templateUrl: './my-dashboards.component.html',
  7. styleUrls: ['./my-dashboards.component.css']
  8. })
  9. export class MyDashboardsComponent {
  10. @HostListener('click')
  11. breadcrumb: any;
  12. lawgroup: string;
  13. constructor(
  14. @Inject(DOCUMENT)
  15. private router: Router,
  16. private ngZone: NgZone
  17. ) {
  18. }
  19. ngOnInit() {
  20. this.breadcrumb = {
  21. 'mainlabel': '法律团队',
  22. 'links': [
  23. {
  24. 'name': '主页',
  25. 'isLink': true,
  26. 'link': '/dashboard/sales'
  27. },
  28. {
  29. 'name': '法律团队仪表盘',
  30. 'isLink': false,
  31. 'link': '#'
  32. },
  33. ]
  34. };
  35. }
  36. goToLawGroupDashboard(lg: string): void {
  37. let fullRoute = '';
  38. this.lawgroup = lg;
  39. fullRoute = 'office/' + this.lawgroup;
  40. this.ngZone.run(() => {
  41. this.router.navigate([fullRoute]);
  42. });
  43. }
  44. }

以下是我的HTML

  1. <a [routerLink]="['/office', 'ABC']" class="h3adjust h3adjust-link" (click)="goToLawGroupDashboard('ABC')" title="单击转到ABC仪表盘">BLG</a>

无论我做什么...我都一直收到相同的错误。为什么?

错误 TypeError: this.router.navigate 不是一个函数
在 my-dashboards.component.ts:50:19
在 _ZoneDelegate.invoke (zone.js:375:26)
在 Object.onInvoke (core.mjs:24210:33)
在 _ZoneDelegate.invoke (zone.js:374:52)

我真的不明白。它在我有的 Angular 14 应用程序中可以工作,但在 15 中却不能。

更新:

移除 @Inject(DOCUMENT) 会消除错误,但这是我的 loadChildren 路由....

但它什么也不做。噢!

  1. {
  2. path: 'office/:id',
  3. component: OfficeComponent
  4. }
英文:

This is a quick question:

Here's my code...

  1. import { DOCUMENT } from &#39;@angular/common&#39;;
  2. import { Component, HostListener, Inject, NgZone } from &#39;@angular/core&#39;;
  3. import { Router } from &#39;@angular/router&#39;;
  4. @Component({
  5. selector: &#39;app-my-dashboards&#39;,
  6. templateUrl: &#39;./my-dashboards.component.html&#39;,
  7. styleUrls: [&#39;./my-dashboards.component.css&#39;]
  8. })
  9. export class MyDashboardsComponent {
  10. @HostListener(&#39;click&#39;)
  11. breadcrumb: any;
  12. lawgroup: string;
  13. constructor(
  14. @Inject(DOCUMENT)
  15. private router: Router,
  16. private ngZone: NgZone
  17. ) {
  18. }
  19. ngOnInit() {
  20. this.breadcrumb = {
  21. &#39;mainlabel&#39;: &#39;Law Groups&#39;,
  22. &#39;links&#39;: [
  23. {
  24. &#39;name&#39;: &#39;Home&#39;,
  25. &#39;isLink&#39;: true,
  26. &#39;link&#39;: &#39;/dashboard/sales&#39;
  27. },
  28. {
  29. &#39;name&#39;: &#39;Law Groups Dashboard&#39;,
  30. &#39;isLink&#39;: false,
  31. &#39;link&#39;: &#39;#&#39;
  32. },
  33. ]
  34. };
  35. }
  36. goToLawGroupDashboard(lg: string): void {
  37. let fullRoute = &#39;&#39;;
  38. this.lawgroup = lg;
  39. fullRoute = &#39;office/&#39; + this.lawgroup;
  40. this.ngZone.run(() =&gt; {
  41. this.router.navigate([fullRoute]);
  42. });
  43. }
  44. }

here's my HTML

  1. &lt;a [routerLink]=&quot;[&#39;/office&#39;, &#39;ABC&#39;]&quot; class=&quot;h3adjust h3adjust-link&quot; (click)=&quot;goToLawGroupDashboard(&#39;ABC&#39;)&quot; title=&quot;Click to go to the ABC Dashboard&quot;&gt;BLG&lt;/a&gt;

No matter what I do... I keep getting the same error. WHY?

ERROR TypeError: this.router.navigate is not a function
at my-dashboards.component.ts:50:19
at _ZoneDelegate.invoke (zone.js:375:26)
at Object.onInvoke (core.mjs:24210:33)
at _ZoneDelegate.invoke (zone.js:374:52)

I just don't understand. It works in an Angular 14 app I have but not in 15.

I just want to pass this "ABC" in the URL and yet, router.navigate() is not a function?

UPDATE:

Removing @Inject (DOCUMENT) took away the error but here's my loadchildren router....

But it goes nowhere. D'oh!

  1. {
  2. path: &#39;office/:id&#39;,
  3. component: OfficeComponent
  4. }

答案1

得分: 3

这一行非常错误:@Inject(DOCUMENT) private router: Router

你将会得到 Document 而不是 Router。移除 @Inject(DOCUMENT),你应该没问题!

英文:

Nah, It couldn't have worked on v14.

This line is very wrong : @Inject(DOCUMENT) private router: Router

You'll get the Document instead of the Router. Remove the

@Inject(DOCUMENT) and you should be fine !

huangapple
  • 本文由 发表于 2023年7月11日 07:45:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定