gpt4 book ai didi

angular - 手动更改路由参数导致用户界面不一致

转载 作者:太空狗 更新时间:2023-10-29 17:28:36 24 4
gpt4 key购买 nike

这里有一个 plunker 可以玩:

https://plnkr.co/edit/qTjftING3Hk2fwN36bQa?p=preview

一切正常,除非您手动更改 url 栏中的 id 参数,因为那时项目下拉列表不会将新项目 ID 显示为当前项目。当用户将 url 保存为收藏链接并将其复制/粘贴到 url 栏时,就会发生这种情况!我会说一个常见的情况!

为了解决这个问题,我可以在 TestsListComponent 中监听 route.params 的变化。并添加带有 sharedService/EventEmitter 的支票该下拉列表中是否存在更改的 ID。 TestsListComponent 中的 bool 返回值 existsProjectId 决定我是否应该重定向到 /projects页面,因为该 ID 不存在。

但老实说,从 TestsListComponent 重定向至少从用户体验的 Angular 来看为时已晚,因为 route projects/:id/tests已经激活。

你会如何纠正这种不当行为?

附言

最佳答案

如下所示更新您的 app.routes.ts 文件并使用 HashLocationStrategy,以供引用:HashLocationStrategy

import {Routes, RouterModule} from '@angular/router';
import ProjectListComponent from './projects-list.component';
import TestsListComponent from './tests-list.component';
import OverviewComponent from './overview.component';
import {LocationStrategy, HashLocationStrategy} from '@angular/common';


export const routes: Routes = [
{ path: '', redirectTo: 'projects', pathMatch: 'full' },
{
path: 'projects', component: ProjectListComponent,
children: [
{ path: ':id', redirectTo: ':id', pathMatch: 'full' },
{
path: ':id', component: OverviewComponent,
children: [
{ path: '', redirectTo: 'tests', pathMatch: 'full' },
{ path: 'tests', component: TestsListComponent },

]
]
}
];

export const appRoutingProviders: any[] = [
{
provide: LocationStrategy,
useClass: HashLocationStrategy
}
];

export const routing = RouterModule.forRoot(routes);

关于angular - 手动更改路由参数导致用户界面不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41474586/

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