Angular 2路由器(ES5)在页面重新加载时不起作用

参见英文答案 > Angular 2.0 router not working on reloading the browser26个
我写了一个简单的Angular 2应用程序.除了一件事,一切都很好.当我点击路由器链接时,更改了Url并加载了组件,然后当我重新加载页面时,我找不到404.无法理解为什么?

这是我的代码

(function(app) {


  app.AppComponent =
    ng.core.Component({
      selector: 'my-app',template: '<a [routerLink]="[\'Component1\']">Component1</a>' + 
                '<a [routerLink]="[\'Component2\']">Component2</a>' + 
                '<router-outlet></router-outlet>',directives: [
            ng.router.ROUTER_DIRECTIVES
      ]
    })
    .Class({
      constructor: function() {}
    });


    app.AppComponent = ng.router.RouteConfig([

    {
        path: '/component1',component: app.Component1,name: 'Component1' 
    },{
        path: '/component2',component: app.Component2,name: 'Component2' 
    }
    ]) ( app.AppComponent );


})(window.app || (window.app = {}));

任何帮助将不胜感激!

这是一个浏览器功能.
认情况下,Angular使用HTML5 pushstate(Angular slang中的PathLocationStrategy).
您需要一个处理所有请求的服务器,例如它请求index.html,或者您切换到HashLocationStrategy(在路由的URL中使用#) https://angular.io/docs/js/latest/api/router/HashLocationStrategy-class.html

另见https://ngmilk.rocks/2015/03/09/angularjs-html5-mode-or-pretty-urls-on-apache-using-htaccess/

要切换到HashLocationStrategy使用

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

确保您拥有所有必需的进口

import {provide} from 'angular2/angular2';
import {
  HashLocationStrategy
  LocationStrategy,ROUTER_PROVIDERS,} from 'angular2/router';

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...