适用于 PC 和移动主页的 Angular 17 路由

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

我从 Angular 16 升级到 17(无独立版),然后迁移到独立版。 它工作正常,但我想要一个更干净的项目,所以我创建了一个新的 Angular 17 项目并复制迁移的项目。我将 app-routing.module.ts 的内容复制到 app.routes.ts 并注释 NgbModule 部分。现在我得到了这个错误:

10.07.54 [vite] Internal server error: window is not defined
  at eval (C:\Code\bengkel_web_17\src\app\app.routes.ts:27:18)
  at async instantiateModule (file:///C:/Code/bengkel_web_17/node_modules/vite/dist/node/chunks/dep-k5wXcrSr.js:54897:9) (x2)

代码是:

{ path: '', 
  redirectTo:  window.screen.width > 768 ? "home" : "mobile", 
  pathMatch: 'prefix'
}

我正在使用可视化代码,在 crl+space 中,这个 window.screen.width 就在那里。 谁能帮我看看出了什么问题吗?

angular angular17
1个回答
0
投票

您已启用 SSR,您可以将其打开

off
但解决此问题的方法是

{ 
    path: '', 
    redirectTo:  window?.screen?.width > 768 ? "home" : "mobile", 
    pathMatch: 'prefix'
}

修复只能解决这个问题,但你应该只关注是否想要 SSR!

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