【问题标题】:Angular2: Not able to navigate to child routes directlyAngular2:无法直接导航到子路线
【发布时间】:2017-01-08 04:25:42
【问题描述】:

我已经创建了这样的路线

{path: 'trainings', component: TrainingWrapperComponent,
  children:[
    {path: '', component: TrainingsListComponent},
    {path: 'createTraining', component: ManageTrainingComponent},
    {path: 'trainingLanguage', component: TrainingLanguageComponent},

  ]
  },

现在,当我从培训导航到子路线时,我可以做到。但是如果我刷新或者我尝试直接打开 url。我无法做到这一点。

例如 trainings/createTraining 会抛出 http 错误,因为它试图通过将 training/ 作为基本 url 来获取资源。

【问题讨论】:

  • 能否把调用路线的代码部分贴出来?
  • 我看不出有什么问题。您在 index.html 中有:?它不是网络服务器而不是不了解路由。使用HashLocationStrategy时是否也会出现?
  • 我是这样导航的:从这里开始工作,但导航后刷新不起作用,当我尝试导航到 localhost/trainings/createTraining 时,它也不起作用
  • @MarcelHoekstra 是的,我在 index.html 中有基本的 href 定义。我也正在使用 browsersync 来运行我的项目。这可能是个问题吗?

标签: angular nested-routes angular-component-router


【解决方案1】:

好的。我终于找到了解决方案。问题是服务器无法将我的子路由识别为角度路由的一部分,因此出现了错误。因此,最简单的解决方案是将用于路由的位置策略更改为 hashlocationstategy。这立即解决了问题。

【讨论】:

  • 同样对我有用,奇怪的是我仍然不能直接去任何 /route 而不做 /#/route 或者这只能通过 ngninx 路由重写来实现?
  • 你能详细说明一下吗?
猜你喜欢
  • 2016-10-29
  • 1970-01-01
  • 2015-10-24
  • 1970-01-01
  • 2017-10-05
  • 1970-01-01
  • 2017-09-21
  • 1970-01-01
  • 2018-08-04
相关资源
最近更新 更多