You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular 17中bootstrapApplication如何配置scrollOffset参数?

在Angular 17中配置scrollOffset的方法

在Angular 17的独立应用模式下,路由配置从RouterModule.forRoot切换为provideRouter搭配各类路由配置器,但scrollOffset依然是withInMemoryScrolling配置对象的属性,直接添加到该对象中即可。

另外你之前用到的initialNavigation和preloadingStrategy,也需要用对应的路由配置器函数来设置:

  • initialNavigation → 使用withInitialNavigation
  • preloadingStrategy → 使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 02:15:17