【问题标题】:Angular route. Problem with route between two view角路线。两个视图之间的路线问题
【发布时间】:2018-12-08 17:12:16
【问题描述】:

您好,我是 Angular 的初学者,我在应用程序中遇到路由问题。我想要有餐厅的路线城市。

{ path: 'city', component: City1Component, children: [
{ path: 'restaurant', component: WenecjaComponent },
] },

结果视图餐厅位于城市景观中。 (图像出现问题): https://ibb.co/82yW1NR

显示:localhost:4200/city/restaurant 但餐厅视图显示在城市视图中。

router-outlet在routerlinks下的component.html中

单击卡片后,我只想将我移动到餐厅的视图中。卡片是带有文本的 2 个框。谁能帮我解决这个问题?

【问题讨论】:

  • 你的图真的不太清楚,能注释一下吗?或者可以用文字具体描述?
  • ibb.co/pjvhDs6希望图片看得懂

标签: angular routing routes router


【解决方案1】:

在您的情况下,您不需要有孩子。您的路线必须像这样定义:

{ path: 'city', component: City1Component },
{ path: 'restaurant', component: WenecjaComponent }

你的app.component.html 上的<router-outlet></router-outlet> 就足够了。

只需像这样<a [routerLink]="['/restaurant']"> 更改您的卡片链接即可从城市更改为餐厅。


如果您需要城市和餐厅之间的关系。您需要在路由中添加另一个组件来处理案例localhost/city/[empty]

这样你就有了路线:

  • localhost/city/[empty]
  • localhost/city/restaurant

为此,您还需要像这样更改路线:

{
  path: 'city',
  component: City1Component,
  children: [
     { path: '', component: [component to create] },
     { path: 'restaurant', component: WenecjaComponent },
  ]
}

欲了解更多信息:https://angular.io/guide/router#child-route-configuration

【讨论】:

  • 谢谢它有效,但我的网址:localhost/restaurant 这是正确的吗?我认为它应该是这样的:localhost/city/restaurant
  • 这取决于您尝试实现的目标。如果您绝对想要cityrestaurant 之间的关系。
  • 是的,我想要城市和餐厅之间的关系,因为当我选择一个城市时,我会看到哪个餐厅与它有关系
猜你喜欢
  • 2010-11-03
  • 1970-01-01
  • 1970-01-01
  • 2011-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多