【问题标题】:Angular 2 router no base href setAngular 2路由器没有基本href集
【发布时间】:2016-04-04 18:15:39
【问题描述】:

我收到一个错误,找不到原因。这是错误:

EXCEPTION: Error during instantiation of LocationStrategy! (RouterOutlet -> Router -> Location -> LocationStrategy).
    angular2.dev.js:23514 EXCEPTION: Error during instantiation of LocationStrategy! (RouterOutlet -> Router -> Location -> LocationStrategy).BrowserDomAdapter.logError @ angular2.dev.js:23514BrowserDomAdapter.logGroup @ angular2.dev.js:23525ExceptionHandler.call @ angular2.dev.js:1145(anonymous function) @ angular2.dev.js:14801NgZone._notifyOnError @ angular2.dev.js:5796collection_1.StringMapWrapper.merge.onError @ angular2.dev.js:5700run @ angular2-polyfills.js:141(anonymous function) @ angular2.dev.js:5719zoneBoundFn @ angular2-polyfills.js:111lib$es6$promise$$internal$$tryCatch @ angular2-polyfills.js:1511lib$es6$promise$$internal$$invokeCallback @ angular2-polyfills.js:1523lib$es6$promise$$internal$$publish @ angular2-polyfills.js:1494(anonymous function) @ angular2-polyfills.js:243microtask @ angular2.dev.js:5751run @ angular2-polyfills.js:138(anonymous function) @ angular2.dev.js:5719zoneBoundFn @ angular2-polyfills.js:111lib$es6$promise$asap$$flush @ angular2-polyfills.js:1305
    angular2.dev.js:23514 ORIGINAL EXCEPTION: No base href set. Please provide a value for the APP_BASE_HREF token or add a base element to the document.BrowserDomAdapter.logError @ angular2.dev.js:23514ExceptionHandler.call @ angular2.dev.js:1154(anonymous function) @ angular2.dev.js:14801NgZone._notifyOnError @ angular2.dev.js:5796collection_1.StringMapWrapper.merge.onError @ angular2.dev.js:5700run @ angular2-polyfills.js:141(anonymous function) @ angular2.dev.js:5719zoneBoundFn @ angular2-polyfills.js:111lib$es6$promise$$internal$$tryCatch @ angular2-polyfills.js:1511lib$es6$promise$$internal$$invokeCallback @ angular2-polyfills.js:1523lib$es6$promise$$internal$$publish @ angular2-polyfills.js:1494(anonymous function) @ angular2-polyfills.js:243microtask @ angular2.dev.js:5751run @ angular2-polyfills.js:138(anonymous function) @ angular2.dev.js:5719zoneBoundFn @ angular2-polyfills.js:111lib$es6$promise$asap$$flush @ angular2-polyfills.js:1305
    angular2.dev.js:23514 ORIGINAL STACKTRACE:BrowserDomAdapter.logError @ angular2.dev.js:23514ExceptionHandler.call @ angular2.dev.js:1157(anonymous function) @ angular2.dev.js:14801NgZone._notifyOnError @ angular2.dev.js:5796collection_1.StringMapWrapper.merge.onError @ angular2.dev.js:5700run @ angular2-polyfills.js:141(anonymous function) @ angular2.dev.js:5719zoneBoundFn @ angular2-polyfills.js:111lib$es6$promise$$internal$$tryCatch @ angular2-polyfills.js:1511lib$es6$promise$$internal$$invokeCallback @ angular2-polyfills.js:1523lib$es6$promise$$internal$$publish @ angular2-polyfills.js:1494(anonymous function) @ angular2-polyfills.js:243microtask @ angular2.dev.js:5751run @ angular2-polyfills.js:138(anonymous function) @ angular2.dev.js:5719zoneBoundFn @ angular2-polyfills.js:111lib$es6$promise$asap$$flush @ angular2-polyfills.js:1305
    angular2.dev.js:23514 Error: No base href set. Please provide a value for the APP_BASE_HREF token or add a base element to the document.
        at new BaseException (angular2.dev.js:8080)
        at new PathLocationStrategy (router.dev.js:1203)
        at angular2.dev.js:1380
        at Injector._instantiate (angular2.dev.js:11923)
        at Injector._instantiateProvider (angular2.dev.js:11859)
        at Injector._new (angular2.dev.js:11849)
        at InjectorDynamicStrategy.getObjByKeyId (angular2.dev.js:11733)
        at Injector._getByKeyDefault (angular2.dev.js:12048)
        at Injector._getByKey (angular2.dev.js:12002)
        at Injector._getByDependency (angular2.dev.js:11990)

有谁知道为什么路由器会抛出这个?我正在使用 angular2 测试版

这是我的代码:

import {Component} from 'angular2/core';
import { RouteConfig, ROUTER_DIRECTIVES } from 'angular2/router';
import {LoginComponent} from './pages/login/login.component';
import {DashboardComponent} from './pages/dashboard/dashboard.component';
@Component({
    selector: 'app',
    directives:[ROUTER_DIRECTIVES],
    template:`
        <div class="wrapper">
            <router-outlet></router-outlet>
        </div>`
})
@RouteConfig([
    { path: '/',redirectTo: '/dashboard' },
    { path: '/login',name:'login',component: LoginComponent },
    { path: '/dashboard',name:'dashboard',component: DashboardComponent,}
])
export class AppComponent {
}

【问题讨论】:

    标签: angular routes href


    【解决方案1】:

    Angular 7,8 修复在 app.module.ts 中

    import {APP_BASE_HREF} from '@angular/common';
    

    在@NgModule 里面添加

    providers: [{provide: APP_BASE_HREF, useValue: '/'}]

    【讨论】:

    • 我在尝试为 Angular 故事书加载组件时遇到此错误。该组件具有路由器依赖性。它解决了错误!
    【解决方案2】:

    检查您的 index.html。 如果您不小心删除了以下部分,请将其包含在内就可以了

    <base href="/">
    
    <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="icon" type="image/x-icon" href="favicon.ico">
    

    【讨论】:

      【解决方案3】:

      只是在 index.html 头标签中添加以下代码

         <html>
          <head>
           <base href="/">
            ...
      

      这对我来说就像一个魅力。

      【讨论】:

        【解决方案4】:

        使用 Angular 4,您可以通过更新 app.module.ts 文件来解决此问题,如下所示:

        在顶部添加import语句如下:

        import {APP_BASE_HREF} from '@angular/common';
        

        并在@NgModule内添加以下行

        providers: [{provide: APP_BASE_HREF, useValue: '/my/app'}]
        

        参考:https://angular.io/api/common/APP_BASE_HREF

        【讨论】:

        • 为什么useValue必须是'/my/app'?
        【解决方案5】:

        我在 Angular4 和 Jasmine 单元测试中遇到过类似的问题;以下给定的解决方案对我有用

        在导入语句下方添加

        import { APP_BASE_HREF } from '@angular/common';
        

        为 TestBed 配置添加以下语句:

        TestBed.configureTestingModule({
            providers: [
                { provide: APP_BASE_HREF, useValue : '/' }
            ]
        })
        

        【讨论】:

          【解决方案6】:

          您还可以通过在 app.module.ts 中包含以下内容来使用基于哈希的导航

          import { LocationStrategy, HashLocationStrategy } from '@angular/common';
          

          并将以下内容添加到 @NgModule({ ... })

          @NgModule({
            ...
            providers:    [
                ProductService, {
                    provide: LocationStrategy, useClass: HashLocationStrategy
                }
            ],
            ...
          })
          

          “HashLocationStrategy——在 URL 中添加井号 (#),井号后的 URL 段唯一标识要用作网页片段的路由。此策略适用于所有浏览器,包括旧浏览器。”

          摘自:Yakov Fain Anton Moiseev。 “使用 TypeScript 进行 Angular 2 开发。”

          【讨论】:

          • 谢谢莱昂内尔!这非常好,解决了我使用上面的 APP_BASE_HREF 解决方案时遇到的问题,该解决方案在使用诸如“product/:id”之类的 url 参数时会失败。谢谢!
          【解决方案7】:

          https://angular.io/docs/ts/latest/guide/router.html

          &lt;head&gt; 标记之后添加基本元素。如果app 文件夹是应用程序根目录,就像我们的应用程序一样,请设置href准确,如下所示。

          &lt;base href="/"&gt; 告诉 Angular 路由器 URL 的静态部分是什么。然后路由器只修改 URL 的剩余部分。

          <head>
            <base href="/">
            ...
          </head>
          

          或者添加

          >= Angular2 RC.6

          import {APP_BASE_HREF} from '@angular/common';
          
          @NgModule({
            declarations: [AppComponent],
            imports: [routing /* or RouterModule */], 
            providers: [{provide: APP_BASE_HREF, useValue : '/' }]
          ]); 
          

          在您的引导程序中。

          在旧版本中,导入必须像

          import {APP_BASE_HREF} from '@angular/common';
          bootstrap(AppComponent, [
            ROUTER_PROVIDERS, 
            {provide: APP_BASE_HREF, useValue : '/' });
          ]); 
          

          import {provide} from 'angular2/core';
          bootstrap(AppComponent, [
            ROUTER_PROVIDERS, 
            provide(APP_BASE_HREF, {useValue : '/' });
          ]); 
          

          import {APP_BASE_HREF} from 'angular2/router';
          

          >= beta.17

          import {APP_BASE_HREF} from 'angular2/platform/common';
          

          另见Location and HashLocationStrategy stopped working in beta.16

          【讨论】:

          • 第二种方式的小例子:bootstrap(AppComponent, [ROUTER_PROVIDERS, provide(APP_BASE_HREF, {useValue : '/'})]);
          • 我必须添加导入行 import {provide} from 'angular2/core'; 才能使用提供。
          • 我必须导入行import {APP_BASE_HREF} from 'angular2/router';
          • 我的项目包含填充了图像模式的 SVG 元素。在文件中设置&lt;base href="/" /&gt; 时,图像永远不会在Firefox 中显示(对于"/""./""#" 或任何其他基本路径,无论图像路径本身在模式中如何指定)。最终奏效的唯一解决方案是改用APP_BASE_HREF。真正的救星!
          • 等一下,但 正是无法通过从本地文件夹(没有 Web 服务器)打开 index.html 来打开 Angular 构建的原因......我不明白为什么 Angular 需要这个基础 href
          【解决方案8】:

          从 2.0 测试版开始 :)

          import { APP_BASE_HREF } from 'angular2/platform/common';
          

          【讨论】:

          • 从 Rc6 开始,这一切都在提供程序中:angular.io/docs/ts/latest/api/common/index/…
          • 当我在 Karma 中的规范因错误而失败时,这对我有所帮助:未设置基本 href。请提供 APP_BASE_HREF 令牌的值或向文档添加基本元素。
          猜你喜欢
          • 2018-05-15
          • 2020-02-02
          • 1970-01-01
          • 2021-05-31
          • 2015-01-22
          • 1970-01-01
          • 1970-01-01
          • 2016-12-22
          • 2016-09-27
          相关资源
          最近更新 更多