Angular 17中bootstrapApplication如何配置scrollOffset参数?
在Angular 17中配置scrollOffset的方法
在Angular 17的独立应用模式下,路由配置从RouterModule.forRoot切换为provideRouter搭配各类路由配置器,但scrollOffset依然是withInMemoryScrolling配置对象的属性,直接添加到该对象中即可。
另外你之前用到的initialNavigation和preloadingStrategy,也需要用对应的路由配置器函数来设置:
initialNavigation→ 使用withInitialNavigationpreloadingStrategy→ 使用withPreloading
修改后的app.config.ts完整配置如下:
// app.config.ts import { ApplicationConfig } from '@angular/core'; import { provideRouter, withInMemoryScrolling, withInitialNavigation, withPreloading } from '@angular/router'; import { PreloadAllModules } from '@angular/router'; import { routes } from './app.routes'; export const appConfig: ApplicationConfig = { providers: [ provideRouter( routes, withInitialNavigation('enabledBlocking'), withInMemoryScrolling({ scrollPositionRestoration: 'enabled', anchorScrolling: 'enabled', scrollOffset: [0, 100] // 在此添加scrollOffset配置 }), withPreloading(PreloadAllModules) ), ], };
这样就能实现和Angular 17之前版本完全一致的路由滚动行为配置。
内容的提问来源于stack exchange,提问作者Justin Ledoux
相关产品推荐
相关产品推荐

