【问题标题】:Issue in Dynamic Routing after domain name in Angular 4+Angular 4+中域名后的动态路由问题
【发布时间】:2018-05-26 23:57:36
【问题描述】:

在我的 Angular 4 项目中,我在路由上有一个容器组件:

    const appRoutes: Routes = [
    { path: '', component: HomeComponent},
    { path: '404', component: NotfoundComponent},
    { path: 'pages/:page-handle', component: StaticpagesComponent },
    { path: 'blog/:blog-handle', component: StaticpagesComponent },
    { path: 'idx', component: ListingComponent },
    { path: 'area/:area-name', component: AreadetailComponent },
    { path: 'zip-code/:zip-name', component: ZipdetailComponent },
    { path: ':agent-handle', component:AgentComponent },
    // otherwise redirect to home
    { path: '**', redirectTo: '404' }
];

所以我想在域名之后访问不同的组件 例如

问题:域后的任何名称都会进入以下路径:

 { path: ':agent-handle', component:AgentComponent },

谁知道我该如何解决?

【问题讨论】:

  • 您找到解决方案了吗?

标签: angularjs angular angular-ui-router angular4-router


【解决方案1】:

路线的顺序很重要。您应该在通配符匹配之前最后定义 '' 路由,以便在没有其他匹配项时匹配。

来自角度路线文档。

配置中路由的顺序很重要,这是设计使然。路由器在匹配路由时使用先匹配获胜策略,因此应将更具体的路由放在不太具体的路由之上。

Source

【讨论】:

  • 是的 @Ankit 你是对的,但是
    { path: ':page-handle', component: StaticpagesComponent }, { path: ':agent-handle', component:AgentComponent },然后总是它首先调用 StaticpagesComponent 然后如何调用 AgentComponent ?如果我通过localhost:8080/:agent-handle
  • 那么你应该创建像agent/:handlepage/:handle这样的路由。
【解决方案2】:

根据您定义的路线,应该是:

http://localhost:8080/zipcode/:zip-name  
http://localhost:8080/area/:area-name  
http://localhost:8080/pages/:page-handle

【讨论】:

  • 你不需要任何改变。例如:如果你运行 localhost:8080/zip-code/27511 ,它会自动进入 ZipDetailComponent
  • 嗨,对不起,我的朋友,我写错了,但我想像 localhost:8080/:zip-name localhost:8080/:area-name 这样我怎样才能得到不同的组件
  • 这可能是不可能的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-13
  • 1970-01-01
  • 2017-10-08
  • 2017-11-22
  • 1970-01-01
  • 2016-08-05
  • 2016-06-11
相关资源
最近更新 更多