Angular 2中带有发布候选组件路由器的嵌套路由
我正在构建一个简单的应用程序,其中有人可以通过电子邮件在表单中搜索用户,然后查看与该用户关联的各种数据 对于顶级AppComponent组件,我有以下路线:Angular 2中带有发布候选组件路由器的嵌套路由,angular,angular2-routing,Angular,Angular2 Routing,我正在构建一个简单的应用程序,其中有人可以通过电子邮件在表单中搜索用户,然后查看与该用户关联的各种数据 对于顶级AppComponent组件,我有以下路线: @Routes([ {path: '/:name', component: CustomerComponent}, {path: '/search', component: SearchComponent} ]) 搜索路径对应于搜索客户的表单,/:name路径用于显示该客户的数据 我的想法是让Customer组件具有一些基本布
@Routes([
{path: '/:name', component: CustomerComponent},
{path: '/search', component: SearchComponent}
])
搜索路径对应于搜索客户的表单,/:name路径用于显示该客户的数据
我的想法是让Customer组件具有一些基本布局,可能还有用户配置文件图片之类的内容,然后具有指向客户配置文件不同部分的链接,如:
@Routes([
{path: '/profile', component: CustomerProfileComponent},
{path: '/loyalty', component: CustomerLoyaltyComponent},
{path: '/coupons', component: CustomerCouponsComponent},
{path: '/settings', component: CustomerSettingsComponent}
])
和html:
<div>
<span>
<a [routerLink]="['./profile']">Profile</a>
<a [routerLink]="['./loyalty']">Loyalty</a>
<a [routerLink]="['./coupons']">Coupons</a>
<a [routerLink]="['./settings']">Settings</a>
</span>
</div>
<div>
<router-outlet></router-outlet>
</div>
我遇到的问题是,在子路由器中,我似乎无法使用RouteSegment来获取用户的电子邮件。例如,在CustomerProfileComponent中,我实现了OnActivate并尝试以下操作:
routerOnActivate(curr: RouteSegment) {
let name = curr.getParam('name');
console.log("profile says " + name);
}
但是,这会打印“配置文件显示未定义”。我想这是因为:name route param不直接属于CustomerComponent,而是属于AppComponent中的路由
我希望能够从RouteSegment中获取每个视图的名称,然后能够使用该名称访问一些api以获取有关该用户的特定数据
我已经读过这样做的一种方法是让一个服务共享状态数据,方法是使用类似于“current user”变量的东西,并将该服务注入子路由中的所有组件中
但是,如果我不需要以这种方式在组件之间共享状态,我认为这会使应用程序更简单
是否有任何方法可以从路由中获取名称,或者是否有更好的方法?您需要在routeLink中添加以下信息
<a [routerLink]="['./profile', profile.name ]">Profile</a>
Profile
其中profile是组件中的属性
此外,可用的URL将是
/profile/joe
或layout/joe
嗨,你能在你的代码中发布一个plunker吗?关于angular2路由器的文档不多,您似乎已经了解了它
<a [routerLink]="['./profile', profile.name ]">Profile</a>