为什么每次运行服务项目时都会显示此错误?

时间:2020-02-29 09:16:35

标签: angular angular-errorhandler

错误:未设置基本href。请提供APP_BASE_HREF的值 标记或向文档添加基本元素。在新的 ProvideLocationStrategy上的PathLocationStrategy(common.js:453) (router.js:5547)在 callFactory(core.js:21286)在 initNgModule(core.js:21168)上的createProviderInstance(core.js:21238) 在createNgModuleRef的新NgModuleRef(core.js:21895) (core.js:21884),位于Object.debugCreateNgModuleRef [as createNgModuleRef](core.js:23715)在 NgModuleFactory .push ../ node_modules/@angular/core/fesm5/core.js.NgModuleFactory_.create (core.js:24419),位于core.js:17765

2 个答案:

答案 0 :(得分:2)

根据您的错误跟踪,看来基本href是未定义的。尝试在index.html中添加<base href="/">

Index.html

<head>
  <base href="/">
  <!-- other code-->
</head> 

希望,这会有所帮助

答案 1 :(得分:1)

预定义的DI令牌,用于与PathLocationStrategy一起使用的基本href。基本href是在生成和识别URL时应保留的URL前缀。

const APP_BASE_HREF: InjectionToken<string>;

以下示例显示了如何使用此令牌为基本应用注入器配置基本href值,以便DI框架可以在应用中的任何位置提供依赖项。

import {Component, NgModule} from '@angular/core';
import {APP_BASE_HREF} from '@angular/common';

@NgModule({
  providers: [{provide: APP_BASE_HREF, useValue: '/my/app'}]
})
class AppModule {}