【发布时间】:2021-01-05 13:41:31
【问题描述】:
我使用以下方法通过不同的路由传递参数,并且我试图在页面刷新或在路由页面上打开另一个选项卡时保留这些参数值。但是,在检索这些参数后,它们会丢失它们的值。那么,是否可以在不使用 :id 等后缀的情况下保留它们的值?在这种情况下,我先打开 SecondComponent,然后使用选项卡打开其名为 SecondChildComponent 的子组件。
first.component.ts
details(name) {
this.router.navigate(['/second'], { state: { name: name} });
}
second.component.ts
constructor(private router: Router) {
this.name= this.router.getCurrentNavigation().extras.state.name;
}
routing.module
const routes: Routes = [
{
path: 'second',
component: SecondComponent,
children: [
{
path: '',
redirectTo: 'second-child',
},
{
path: 'second-child',
component: SecondChildComponent
}
]
}
];
【问题讨论】:
-
你尝试使用localStorage了吗?这是浏览器上的键值存储。您可以使用 localStorage.setItem('key', 'value') 访问它。您不能直接存储文字对象,但可以执行 localStorage.setItem('key', JSON.stringify(object)) 然后检索它 JSON.parse(localStorage.getItem('key'))
-
@IvanLynch Hola Amigo。其实我想用它,但我不确定它是否常用于这种场景,也找不到这个场景的例子。你会建议我的情况吗?您能否发布一个示例代码作为答案?格拉西亚斯。
标签: javascript angular routes angular-routing angular-router