“错误:未设置基本 href。” 升级到 Angular 8 后

Cod*_*lla 5 angular

完成从 Angular 5.x 到 8.0.1 的更新后,我的应用程序可以编译,但在运行时出现以下错误(使用ng serve):

错误:未设置基本 href。请为 APP_BASE_HREF 令牌提供值或向文档添加基本元素。

我看过其他帖子,例如:Angular 2 router no base href set,但是,我的应用程序中已经包含 HTML 基本元素。我index.html的如下:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>MyApp</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<app-root></app-root>
</html>
Run Code Online (Sandbox Code Playgroud)

我可以通过添加来解决问题:

{
  provide: APP_BASE_HREF,
  useValue: '/'
}
Run Code Online (Sandbox Code Playgroud)

到我的app.modules.ts,但我更感兴趣的是了解为什么现在需要这样做,当使用ng new该提供程序创建的裸机应用程序在没有此提供程序的情况下工作时。

Con*_*Fan 6

index.html通过添加body元素来确保这是一个有效的 HTML 文件,您可以在其中插入 Angular 根组件:

<html lang="en">
<head>
  <base href="/">
  ...
</head>
<body>
  <app-root></app-root>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)