HashLocationStrategy 在 Angular2 RC1 中不起作用



我正在尝试解决刷新 Angular2 页面导致 404 错误的问题。当我尝试实现HashLocationStrategy时,它似乎对生成的URL没有任何影响。

我的主.ts文件是:

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})
]);

考虑暂时使用已弃用的路由器。当前路由器从未完成,正在替换为新的路由器。

而不是使用hashbang方法,使用服务器配置解决。添加了 .htaccess 文件:

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

尝试添加

<base href="#">

index.html <head></head>部分内。

最新更新