我有一个组件,它使用路由器导航到另一个带有布尔变量的组件,如下所示
this.router.navigate(['/consume/course/' + this.id, {state: { isFirst }} ], { replaceUrl: true });
在另一个组件中,我试图像这样从路由器获取这个变量
const isFirst = this.router.getCurrentNavigation().extras.state.isFirst;
但我收到此错误:
无法读取 null 的属性“额外”
为什么当前导航为空?
另外,这是我的第二个组件的路由模块
const routes: Routes = [
{
path: 'course/:id',
component: CourseComponent,
canActivate: [RedirectGuard],
data: { menuType: 'test' },
},
];
据我所知,添加时state
对于导航,Angular 正在将您的值附加到窗口History
目的。您可以像这样检索该值:
const { isFirst } = window.history.state;
你可以在这里阅读更多在 Angular 中导航期间将数据保存在状态对象中
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)