多个路由指向同一个 Angular 组件是否会影响性能?
Is there any performance hit for having multiple routes pointing to the same Angular component?
我想让多个路由指向同一个组件,这样我就可以更改组件的默认行为。
{ path: 'city/:zipCode/:cityState', component: JobSearchComponent },
{ path: 'company/:organizationId/:companyName', component: JobSearchComponent }
在 运行 时间或任何时候让多个路由指向单个组件(如内存中的多个实例)是否有任何性能损失?
不会有太大的影响,因为Angular仅在路由激活时实例化组件。
这就是为具有活动路由的组件调用生命周期方法的原因。我们可以用 ngOnInit
和 ngOnDestroy
.
等简单方法来确认这一点
在内部,路由器的工作方式类似于将这些组件添加到 entryComponents
数组。这样即使稍后在运行时激活路由,angular 编译器也知道将它们包含在构建中。
另一件要记住的事情是 Angular 从根开始对路由器数组中的路由进行首次匹配。因此,我们添加路由的顺序很重要。常见的例子是我们经常声明 not-found
或不匹配,即 **
路径位于路由数组的末尾。
来自 Angular 文档 @ https://angular.io/guide/router#configuration:
The order of the routes in the configuration matters and this is by
design. The router uses a first-match wins strategy when matching
routes, so more specific routes should be placed above less specific
routes. In the configuration above, routes with a static path are
listed first, followed by an empty path route, that matches the
default route. The wildcard route comes last because it matches every
URL and should be selected only if no other routes are matched first.
考虑到上述情况,路径匹配的唯一性能将是 O(N)。
我想让多个路由指向同一个组件,这样我就可以更改组件的默认行为。
{ path: 'city/:zipCode/:cityState', component: JobSearchComponent },
{ path: 'company/:organizationId/:companyName', component: JobSearchComponent }
在 运行 时间或任何时候让多个路由指向单个组件(如内存中的多个实例)是否有任何性能损失?
不会有太大的影响,因为Angular仅在路由激活时实例化组件。
这就是为具有活动路由的组件调用生命周期方法的原因。我们可以用 ngOnInit
和 ngOnDestroy
.
在内部,路由器的工作方式类似于将这些组件添加到 entryComponents
数组。这样即使稍后在运行时激活路由,angular 编译器也知道将它们包含在构建中。
另一件要记住的事情是 Angular 从根开始对路由器数组中的路由进行首次匹配。因此,我们添加路由的顺序很重要。常见的例子是我们经常声明 not-found
或不匹配,即 **
路径位于路由数组的末尾。
来自 Angular 文档 @ https://angular.io/guide/router#configuration:
The order of the routes in the configuration matters and this is by design. The router uses a first-match wins strategy when matching routes, so more specific routes should be placed above less specific routes. In the configuration above, routes with a static path are listed first, followed by an empty path route, that matches the default route. The wildcard route comes last because it matches every URL and should be selected only if no other routes are matched first.
考虑到上述情况,路径匹配的唯一性能将是 O(N)。