繁体   English   中英

如何使用Angular 1.5正确路由SPA?

[英]How to properly routing for SPA using Angular 1.5?

我是新手。 我创建了一个博客来显示我的个人资料。 在我的网站中,我有几个组件,而我的app.module具有路由配置,如下所示:

app.config(function($locationProvider, $routeProvider){
  $locationProvider.html5Mode({
    enabled:true,
    requireBase: false
  })

  $routeProvider.
    when("/", {
      template: "<research-overview></research-overview>"
    }).
    when("/research-interests", {
      template: "<research-interests></research-interests>"
    }).
    otherwise({
      redirectTo: "/"
    })
 });

这是我的组件的一个示例:

angular.module('researchOverview', [])
  .component('researchOverview', {
     templateUrl: '/templates/research-overview.html',
});

我尝试使用python -m SimpleHttpServer在本地运行服务器,以便可以使用浏览器访问本地网站到localhost:8000 ,当我单击主页上的链接时,页面已正确加载。 但是,当我最初使用不同的URL(例如localhost:8000/testing )访问我的网站时,Angular路由似乎无法正常工作。 我得到这个错误,

在此处输入图片说明

如何设置路由器,以便可以将这些URL重定向到我的主页?

考虑到您已启用html5模式,我认为您可能需要设置服务器,以便无论路径如何,它都将返回index.html文件。

例如,当您的服务器在localhost:8000 / test处收到对资源的​​请求时,它将返回index.html。

从AngularJS文档中:

使用此模式需要在服务器端重写URL,基本上,您必须重写指向应用程序入口点的所有链接(例如index.html)。 对于这种情况,要求标签也很重要,因为它使Angular能够区分作为应用程序基础的url部分和应由应用程序处理的路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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