分配默认值路由参数时遇到问题
我在为组件 ts 类内部的路由参数分配默认值时遇到问题。我希望 genericId 默认为 null。我希望货币化、排序依据和页面具有默认值。我不确定我是否应该在 ngOninit 内部执行此操作,或者是否应该在我的应用程序路由模块内部以某种方式执行此操作。
export class MoviesPageComponent implements OnInit {
movies?: MovieData;
genreId?: string | null = null;
monetization?: string | null;
sortBy?: string | null;
page?: string | null;
constructor(private getMovies: GetMoviesService,
private route: ActivatedRoute) { }
ngOnInit(): void {
this.route.queryParamMap.subscribe((params: ParamMap) => {
this.genreId = params.get('genreId')
})
this.route.paramMap.subscribe((params: ParamMap) => {
this.monetization = params.get('monetization');
this.sortBy = params.get('sortBy');
this.page = params.get('page');
});
if (this.genreId === null) {
this.getMovies.moviesData(this.monetization, this.sortBy, this.page).subscribe((movieData: MovieData) => {
if (movieData !== undefined) {
this.movies = movieData;
}
})
}
}
}
我的应用程序路由模块数组如下所示。
const routes: Routes = [
{path: ' ', pathMatch: 'full', redirectTo: '/:monetization/:sortBy/:page'},
{path: '/:monetization/:sortBy/:page/', pathMatch: 'full', component: MoviesPageComponent}
];
I'm having trouble assigning default values to my route parameters inside of my component ts class. I want the genreId to be null by default. I want monetization, sortBy, and page to have default values. I'm not sure if I'm supposed to do this inside of ngOninit or if I'm supposed to do this somehow inside of my app routing module.
export class MoviesPageComponent implements OnInit {
movies?: MovieData;
genreId?: string | null = null;
monetization?: string | null;
sortBy?: string | null;
page?: string | null;
constructor(private getMovies: GetMoviesService,
private route: ActivatedRoute) { }
ngOnInit(): void {
this.route.queryParamMap.subscribe((params: ParamMap) => {
this.genreId = params.get('genreId')
})
this.route.paramMap.subscribe((params: ParamMap) => {
this.monetization = params.get('monetization');
this.sortBy = params.get('sortBy');
this.page = params.get('page');
});
if (this.genreId === null) {
this.getMovies.moviesData(this.monetization, this.sortBy, this.page).subscribe((movieData: MovieData) => {
if (movieData !== undefined) {
this.movies = movieData;
}
})
}
}
}
My app routing module array looks like this.
const routes: Routes = [
{path: ' ', pathMatch: 'full', redirectTo: '/:monetization/:sortBy/:page'},
{path: '/:monetization/:sortBy/:page/', pathMatch: 'full', component: MoviesPageComponent}
];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需在声明它们时将它们设置为默认值即可。如果收到的值是真值,则覆盖默认值。如果第一个值是 false,我在赋值期间使用
||
运算符转到下一个值。考虑使用ActivatedRouteSnapshot
而不是ActivatedRoute
来获取参数。附带说明一下,如果您没有理由区分
null
和undefined
,只需使用undefined
并检查该值是否为 falsey与!
或 true 与简单的 if 语句。确实有助于整理代码。Just set them to a default value when you declare them. Overwrite the default value if the value received is truthy. I use the
||
operator during assignment to go to the next value if the first one is falsey. Consider usingActivatedRouteSnapshot
instead ofActivatedRoute
to get params.As a side note, if you don't have a reason to differentiate between
null
andundefined
just useundefined
and check if the value is falsey with!
or truthy with a simple if statement. Really helps declutter code.