微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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';

原文地址:https://www.jb51.cc/angularjs/143615.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐