import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, ParamMap, Params } from '@angular/router';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
@Component({
template: '<h1>{{ title$ | async }}</h1>',
})
export class View1Component implements OnInit {
public title$: Observable<string>;
constructor(private _route: ActivatedRoute) {
}
ngOnInit() {
console.log('view1 init');
this.title$ = this._route.params.pipe(
map((value: Params) => {
console.log(value);
return value['title'] || 'no title';
})
);
}
}
我有一个像上面这样的简单视图组件。组件将使用
<router-outlet>.
这个
路线
模块文件中的配置如下所示。
export const appRoutes: Routes = [
{ path: 'view1', component: View1Component },
{ path: 'view1/:title', component: View1Component }
];
我正在努力实现这个组件的单元测试。更具体地说,我不知道如何正确地注入
ActivatedRoute
组件中的值,以及如何处理异步方案。