不清楚的路线“pathMatch”重大更改升级到 Angular 14

问题描述 投票:0回答:1

https://update.angular.io/?l=3&v=13.0-14.0 关于迁移到 Angular 14 的 Angular 升级说明之一是“如果您使用 pathMatch 定义路由,则可能必须将其转换为显式路由或路由。Route.pathMatch 不再与字符串类型兼容。”我可能需要投射什么

第二句与https://angular.io/api/router/Route处的接口声明相矛盾,即:

pathMatch?: 'prefix' | 'full'

所以,它仍然是字符串类型。如何将其投射到一个或多个路线?

更具体地说,如果我现有的

routes
定义开始

const routes: Routes = [
  { path: '', pathMatch: 'full', component: HomeComponent },

那么升级说明告诉我我必须做什么?也许注释中的“它”指的是路线,我需要投射

  { path: '', pathMatch: 'full', component: HomeComponent }

到路由,但这与“不再与字符串类型兼容”有什么关系?我的意思是,“完整”是一个字符串并且它与类型规范匹配

'prefix' | 'full'
,那么这里有什么问题?

angular angular-router
1个回答
0
投票

您需要做的就是转动您当前的代码:

    const routes: Routes = [
  { path: '', pathMatch: 'full', component: HomeComponent },

对于以下代码段,因为角度 1 将其显式转换为 Route 或 Routes。 :

  const routes: Routes = [
      { path: '', pathMatch: 'full' as Route['pathMatch'], component: HomeComponent },

不要忘记导入必要的 Route 或 Routes 导入以使这项工作正常进行,祝你好运。如果尝试过,这对我来说非常有效

© www.soinside.com 2019 - 2024. All rights reserved.