作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个路由模块,它为许多其他模块设置了路由,我已经成功地使用 activatedRoute
来实现,没有任何问题。但我现在创建了一个单一组件,当我尝试使用 activatedRoute
时,它返回一个空对象。
路由模块看起来像这样:
const routes = [
{
path: 'path',
component: myComponent,
resolve: { resolver: myCustomResolver },
children: [
{
path: '',
children: [
{
path: 'users',
loadChildren: 'app/users.module#UsersModule'
},
{
path: 'articles',
loadChildren: 'app/articles.module#ArticlesModule'
}
]
},
// this is the new component
{ path: 'stories', component: StoriesComponent }
]
}
]
我在模块和新组件中对 activatedRoute
使用相同的方法:
export class StoriesComponent implements OnInit {
private routeData;
constructor(private activatedRoute: ActivatedRoute) {}
ngOnInit() {
// this is returning an empty object '{}'
this.activatedRoute.data.subscribe(data => {
this.routeData = data;
});
}
}
如果您能提供任何帮助,我们将不胜感激。
最佳答案
您想要访问子级中的父级解析数据。试试这个。
this.routeData = this.activatedRoute.parent.snapshot.data;
关于angular - activateRoute 返回空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68631543/
我有一个路由模块,它为许多其他模块设置了路由,我已经成功地使用 activatedRoute 来实现,没有任何问题。但我现在创建了一个单一组件,当我尝试使用 activatedRoute 时,它返回一
我正在使用带有 Jest 的 Angular 7 并且正在模拟组件的提供者。很多时候我需要更改在 TestBed 之后注入(inject)到组件中的内容。已编译,并且使用此代码执行此操作没有任何问题:
我正在使用 ActiveRoute 从 url 读取一些值。 http://localhost:4200/project-test/#/add?orderId=156&customerNumber=4
我是一名优秀的程序员,十分优秀!