简体   繁体   中英

HashLocationStrategy has no effect in Angular2 RC1

I am trying to solve the problem where refreshing an Angular2 page results in a 404 error. When I try to implement HashLocationStrategy, it doesn't seem to make any difference to the URLs which are generated.

My main.ts file is:

import { bootstrap }    from '@angular/platform-browser-dynamic';
import { provide } from '@angular/core';
import { AppComponent } from './app.component';
import { ROUTER_PROVIDERS } from '@angular/router';
import { LocationStrategy, HashLocationStrategy } from '@angular/common';

bootstrap(AppComponent, [
  ROUTER_PROVIDERS,
  provide(LocationStrategy, {useClass: HashLocationStrategy})
]);

Consider using the deprecated router for the time being. The current router was never finished and is being replaced with a new, new router.

Instead of using the hashbang approach, solved using server config. Added .htaccess file:

RewriteEngine On  
  # If an existing asset or directory is requested go to it as it is
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
  RewriteRule ^ - [L]

  # If the requested resource doesn't exist, use index.html
  RewriteRule ^ /index.html

Try to add

<base href="#">

inside <head></head> section in your index.html .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM