gpt4 book ai didi

Angular Router.navigate 使用 queryParams 导航到子路由

转载 作者:行者123 更新时间:2023-12-03 16:13:43 28 4
gpt4 key购买 nike

Angular.Router.navigate 无法使用 queryParams 导航到子路由

А已经尝试过:

this.router.navigateByUrl('/desktop/search?q=folder'));

this.router.navigate(['desktop', 'search'], { queryParams: {q: 'folder'} });

this.router.navigate(['desktop/search'], { queryParams: {q: 'folder'} });

我的路线:
{
path: 'desktop',
component: FavoritesPageComponent,
children: [
{ path: 'desktop-admin', component: DesktopAdminComponent },
{ path: 'favorites', component: FavoritesBodyMainComponent },
{ path: 'sessions', component: SessionsComponent },
{ path: 'search', component: FavoritesBodySearchComponent },
{ path: 'shared_with_me', component: FavoritesBodySharedComponent },
{ path: 'recycle', component: FavoritesBodyRecycleComponent }
]
}

当我尝试导航到“桌面/搜索?q=文件夹”时,出现以下错误:
ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'desktop/search%3Bq%3D%25D0%25BF%25D0%25B0%25D0%25BF%25D0%25BA%25D0%25B0'

怎么了?有没有办法使用带有普通查询参数的子路由,比如
.../desktop/search?q=folder

this.route.queryParams.subscribe(params => {
console.log('params['q']: ', params['q']);
});

最佳答案

看这个例子:

1-声明路由参数:

// app.routing.ts    
export const routes: Routes = [
{ path: '', redirectTo: 'product-list', pathMatch: 'full' },
{ path: 'product-list', component: ProductList },
{ path: 'product-details/:id', component: ProductDetails }
];

要查看 ID 为 10 的产品的产品详细信息页面,您必须使用以下 URL:
localhost:4200/product-details/10 // it's not this -> /product-details?id:10

2-使用参数链接到路线:
<a [routerLink]="['/product-details', 10 or variable name]">
title
</a>

或者
<a (click)="goToProductDetails($event,10)">
title
</a>

// into component.ts
goToProductDetails(e,id) {
e.preventDefault();
this.router.navigate(['/product-details', id]);
}

3-读取路由参数:
// into component.ts

constructor(private route: ActivatedRoute) {}

ngOnInit() {
this.route.params.subscribe(params => {
this.id = +params['id'];
});
}

我希望这可以帮助你。

关于Angular Router.navigate 使用 queryParams 导航到子路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55333580/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com