Angular 2 router no base href set

前端 未结 8 984
半阙折子戏
半阙折子戏 2020-11-22 15:19

I am getting an error and can\'t find why. Here is the error:

EXCEPTION: Error during instantiation of LocationStrategy! (RouterOutlet -> Router -> Loc         


        
8条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 15:48

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

    Add the base element just after the tag. If the app folder is the application root, as it is for our application, set the href value exactly as shown here.

    The tells the Angular router what is the static part of the URL. The router then only modifies the remaining part of the URL.

    
      
      ...
    
    

    Alternatively add

    >= Angular2 RC.6

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

    in your bootstrap.

    In older versions the imports had to be like

    < Angular2 RC.6

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

    < RC.0

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

    < beta.17

    import {APP_BASE_HREF} from 'angular2/router';
    

    >= beta.17

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

    See also Location and HashLocationStrategy stopped working in beta.16

提交回复
热议问题