我试图实现一个简单的角度路线
const appRoutes: Routes = [
{ path: '' ,redirectTo: '/recipes', pathMatch: 'full'},
{ path: 'recipes' , component: RecipesComponent},
{ path: 'recipes/:recipeName' , component: RecipesComponent},
];
在一些生成列表的组件中,我有一个单击a的功能 更新状态变量和路由,虽然我也在初始化时发生了抓取机制订阅。
ngOnInit() {
this.route.params
.subscribe(
(params: {Params}) => {
this.selectedRecipe = this.findRecipeByName(params['recipeName']);
});
}
navRecipe(recipeName: string): void {
let path = '/recipes/' + recipeName;
this.selectedRecipe = this.findRecipeByName(recipeName);
this.router.navigate([path]);
}
但是当我尝试点击一个重新路由到带参数的组件的链接时,我得到以下内容:
core.js:1350 ERROR Error: Uncaught (in promise): EmptyError: no elements in sequence
EmptyError: no elements in sequence
at new EmptyError (EmptyError.js:28)
at FirstSubscriber._complete (first.js:154)
at FirstSubscriber.Subscriber.complete (Subscriber.js:122)
at MergeMapSubscriber._complete (mergeMap.js:150)
at MergeMapSubscriber.Subscriber.complete (Subscriber.js:122)
at MapSubscriber.Subscriber._complete (Subscriber.js:140)
at MapSubscriber.Subscriber.complete (Subscriber.js:122)
at EmptyObservable._subscribe (EmptyObservable.js:83)
at EmptyObservable.Observable._trySubscribe (Observable.js:172)
at EmptyObservable.Observable.subscribe (Observable.js:160)
at new EmptyError (EmptyError.js:28)
at FirstSubscriber._complete (first.js:154)
at FirstSubscriber.Subscriber.complete (Subscriber.js:122)
at MergeMapSubscriber._complete (mergeMap.js:150)
at MergeMapSubscriber.Subscriber.complete (Subscriber.js:122)
at MapSubscriber.Subscriber._complete (Subscriber.js:140)
at MapSubscriber.Subscriber.complete (Subscriber.js:122)
at EmptyObservable._subscribe (EmptyObservable.js:83)
at EmptyObservable.Observable._trySubscribe (Observable.js:172)
at EmptyObservable.Observable.subscribe (Observable.js:160)
at resolvePromise (zone.js:824)
at resolvePromise (zone.js:795)
at eval (zone.js:873)
at ZoneDelegate.invokeTask (zone.js:425)
at Object.onInvokeTask (core.js:4621)
at ZoneDelegate.invokeTask (zone.js:424)
at Zone.runTask (zone.js:192)
at drainMicroTaskQueue (zone.js:602)
at ZoneTask.invokeTask [as invoke] (zone.js:503)
at invokeTask (zone.js:1540)
答案 0 :(得分:24)
我在Angular v 4.4.6中遇到了同样的错误。我找到了两种解决错误的替代解决方案。一个是为每个路由定义添加pathMatch: 'full'
。另一个是将rxjs降级到v5.5.3以下。如果一个人不适合你,也许另一个人会。
更新:似乎问题已在rxjs v5.5.5中修复。在使用Angular 5.0.5的项目中,将rxjs升级到v5.5.5后,错误停止发生。
答案 1 :(得分:15)
Angular最新版本错误。将pathMatch: 'full'
添加到所有路由。
{
path: '',
component: HomeComponent,
pathMatch: 'full'
},
{
path: 'about',
component: AboutComponent,
pathMatch: 'full'
},
答案 2 :(得分:1)
我在Angular 5.0.1和rxjs 5.5.3中遇到了同样的错误。当我们更改routes数组中的顺序时,我们没有遇到异常。所以对你的例子来说:
const appRoutes: Routes = [
{ path: '', redirectTo: '/recipes', pathMatch: 'full'},
{ path: 'recipes/:recipeName', component: RecipesComponent},
{ path: 'recipes', component: RecipesComponent},
];
在一般路径(recipes/:recipeName
)之前放置更详细的路径(recipes
)。