【问题标题】:HashLocationStrategy has no effect in Angular2 RC1HashLocationStrategy 在 Angular2 RC1 中无效
【发布时间】:2016-06-04 17:16:11
【问题描述】:

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

我的 main.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})
]);

【问题讨论】:

    标签: angular


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      不是使用 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
      

      【讨论】:

        【解决方案3】:

        尝试添加

        <base href="#">

        在您的index.html 中的<head></head> 部分内。

        【讨论】:

        • 我觉得应该是<base href="/"># 是干什么用的?
        • @GünterZöchbauer # 应该作为锚点,所以没有“真正的”重定向
        猜你喜欢
        • 2016-06-07
        • 1970-01-01
        • 1970-01-01
        • 2017-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多