【问题标题】:Using same URL for multiple diferent ROUTES - Angular 2+对多个不同的 ROUTES 使用相同的 URL - Angular 2+
【发布时间】:2019-01-05 23:42:16
【问题描述】:

我需要制作一个 Angular 应用程序,以便为多个路由使用相同的 url。示例:

路线 1。而不是导航到:

/合作伙伴/url参数

导航到:

/partnerurl参数

路线 2。而不是导航到:

/用户/url参数

导航到:

/userurl参数

其中路径是“/”,后跟一个动态值,即参数。

有没有办法做到这一点?

这是我的路线的配置方式:

{ 路径:'/partner/:partnerId',组件:PartnerComponent },

{ 路径:'/customer/:customerId',组件:CustomerComponent },

我想要这样的东西:

{ 路径:'/partner/:partnerId',组件:PartnerComponent },

{ 路径:'/:customerId',组件:CustomerComponent },

【问题讨论】:

  • 你能显示你的路线配置吗
  • 嗨 Chellappan,我已经编辑了我的帖子并添加了我的路线

标签: javascript angular angular-ui-router angular2-routing


【解决方案1】:

这是您想要实现的目标的示例,follow the link 也是如此。 如果有任何变化,请告诉我。

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { RouterModule, Routes } from '@angular/router';

import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';
import { CustomerComponent } from '../customer/customer.component';
import {PartnerComponent} from '../partner/partner.component';

export const routes: Routes = [
  { path: 'partner/:partnerId', component: PartnerComponent },
  { path: ':customerId', component: CustomerComponent },
  { path: '', component: HelloComponent, pathMatch:'full' },
];

@NgModule({
  imports: [BrowserModule, FormsModule, RouterModule.forRoot(routes)],
  declarations: [AppComponent, HelloComponent, CustomerComponent, PartnerComponent],
  bootstrap: [AppComponent]
})
export class AppModule { }

【讨论】:

  • 嗨!非常感谢您的回复。问题是我需要两条路线都指向同一个网址。像 /:partnerId 和 /:customerId。我还有其他路线,所以他们也需要继续努力。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-22
  • 2017-06-15
  • 1970-01-01
相关资源
最近更新 更多