【问题标题】:Error: Cannot match any routes. URL Segment on page refresh when the page refreshes错误:无法匹配任何路由。页面刷新时页面刷新时的 URL 段
【发布时间】:2020-06-23 08:59:22
【问题描述】:

我有一个 Angular 8 应用程序,它从 SQLite 数据库加载设备并将它们全部显示在一个名为“monitoring”的 url 中。当我希望用户查看有关当前设备的详细信息时,他会被重定向到“”monitoring/:id”,一切正常!但是,如果用户刷新 url “monitoring/:id”,则所选设备为空,我得到以下信息错误“未捕获(承诺中):错误:无法匹配任何路由。 URL 段:'123'"。这是根的路由模块

app.module.ts

RouterModule.forRoot([

  { path: '', redirectTo: '/monitoring', pathMatch: 'full' }, 
  { path: 'monitoring', component: AppMonitoring, pathMatch: 'full', data: { state: 'monitor' }, },
  { path: "monitoring/:id", component: DeviceMeasurements,  },
  { path: 'management', component: AppManagement, pathMatch: 'full', data: { state: 'manage' } },
  { path: 'system', component: AppSystemSettings, pathMatch: 'full', data: { state: 'system' }, },
  { path: 'notifications', component: AppSettingsNotifications, pathMatch: 'full', data: { state: 'notifications' }, },

], { useHash: true }
)],

请告诉我是否需要更多信息以及如何解决此问题? 感谢您的宝贵时间!

编辑:这是重定向 url 的 HTML

<a class="pull-left" href="#" (click)="DeviceDelete()" onclick="return false;" matTooltip="{{language_service.getText(this.language_service.ITEMS.DEVICE_DELETE)}}">         <i class="nav-icon fa fa-trash"></i>   </a>

ViewMeasurements 的内容

  ViewMeasurements() {
    this.device_service.SetCurrentDevice(this.device);
    this.router.navigateByUrl(`/monitoring/${this.device.id}`);
  }

SetCurrentDevice 的内容

  SetCurrentDevice(device: DeviceInterface) {
    this.InitCurrentDevice.next(device);
  }

我还有一个 GetCurrentDevice 函数,它在动态 id 页面中调用

  GetCurrentDevice(): DeviceInterface  {
    return this.InitCurrentDevice.getValue();
  }

并在动态页面中这样使用

  ngOnInit() {
      this.device = this.device_service.GetCurrentDevice(); 
  }

我在 ngOnInit 函数中尝试了以下操作,但结果相同

    let n = window.location.href.lastIndexOf('/');
    let id = window.location.href.substring(n + 1);
    if(this.device==null){
        this._router.navigateByUrl('/monitoring', { skipLocationChange: true }).then(() => {
    
          this._router.navigate(['/monitoring/' + id]);
        });

}

【问题讨论】:

  • 您最初是如何获取设备 ID 的?刷新应用程序后,它会丢失之前加载的所有数据。
  • 您可以检查DeviceMeasurements内部是否有id并且没有可以将其重定向到监控路由的数据
  • 在我不想重定向的 deviceID 页面上,我还添加了更详细的说明如何设置/获取 CurrentDevice。还有什么我试图阻止它

标签: angular typescript routes


【解决方案1】:

将此路径 { path: '', redirectTo: '/monitoring', pathMatch: 'full' } 放在路径对象的底部。

【讨论】:

  • 这部分解决了问题,页面加载但错误“错误:未捕获(承诺):错误:无法匹配任何路由。URL段:'123'”仍然存在。
  • 能否去掉this._router.navigate(['/monitoring/' + id])中的正斜杠(/);
  • 没有正斜杠就无法工作,显示以下错误 - “无法匹配任何路由。URL 段:'monitoring123'”
  • 您好,创建诸如“ management:id ”之类的路由一切正常,所以这个答案帮助我解决了问题
猜你喜欢
  • 1970-01-01
  • 2023-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-09
  • 2022-01-05
  • 1970-01-01
  • 2021-06-15
相关资源
最近更新 更多