- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 ActivatedRoute
从父组件获取子路由数据('layout') .我已经尝试了以下示例,并且能够获得我正在寻找的数据,但只能获得一次。在子路由更改期间,我没有看到更新的“布局”值。
路线
const routes: Routes = [
{
path: '',
component: Layout1Component,
children: [
{
path: '',
redirectTo: 'home',
pathMatch: 'full'
},
{
path: 'home',
loadChildren: './pages/analytics/analytics.module#AnalyticsModule',
data: {
layout: 'boxed'
}
},
{
path: 'files',
loadChildren: './pages/files/files.module#FilesModule',
data: {
layout: 'full'
}
}
]
}
];
export class Layout1Component implements OnInit {
constructor(private service: AppService, route: ActivatedRoute) {
this.layout = route.snapshot.firstChild.data['layout'];
route.url.subscribe(() => {
console.log(route.snapshot.firstChild.data['layout']);
});
}
}
最佳答案
根据我的理解,您只获取一次数据的原因是因为 ActivatedRoute
获取 url
作为 行为主题 当它被实例化时。
function createActivatedRoute(c: ActivatedRouteSnapshot) {
return new ActivatedRoute(
new BehaviorSubject(c.url), new BehaviorSubject(c.params), new BehaviorSubject(c.queryParams),
new BehaviorSubject(c.fragment), new BehaviorSubject(c.data), c.outlet, c.component, c);
}
const routes: Route[] = [
{
path: '',
component: HelloComponent,
children: [
{
path: 'foo',
loadChildren: () => import('path/to/foo').then(m => m.FooModule),
data: {
name: 'andrei'
}
},
{
path: 'bar',
loadChildren: () => import('path/to/bar').then(m => m.BarModule),
data: {
name: 'john'
}
},
]
},
]
constructor (private router: Router, private route: ActivatedRoute) { }
ngOnInit () {
const routeEndEvent$ = this.router.events
.pipe(
filter(e => e instanceof NavigationEnd),
tap(() => console.warn("END")),
);
this.router.events
.pipe(
filter(e => e instanceof ChildActivationEnd && e.snapshot.component === this.route.component),
buffer(routeEndEvent$),
map(([ev]) => (ev as ChildActivationEnd).snapshot.firstChild.data),
takeUntil(this.ngUnsubscribe$)
)
.subscribe(childRoute => {
console.log('childRoute', childRoute);
})
}
routeEndEvent$
是一个 observable,只有在路由准备好时才会发出 ( NavigationEnd
) filter(e => e instanceof ChildActivationEnd && e.snapshot.component === this.route.component)
:在检查了路由器发出的事件一段时间后,我达到了这个解决方案,以确定组件( HelloComponent
)是否是更改的路由( this.route.component
)的父级( e.snapshot.component
)。 buffer(routeEndEvent$)
:我们只想在路由器达到其最终状态( NavigationEnd
)时采取行动。 here您可以阅读有关 buffer
的更多信息运算符(operator) 关于angular - ActivatedRoute 不会在路线更改时更新子路线数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58681856/
我有两种类型的路由 Public 和 Private。 只有用户登录后才能访问所有私有(private)路由: return tokenService.token ? ( <>
我已按照 Laravel 5.5 文档在我们的应用程序上要求、安装和配置 Laravel Passport。我们仅使用密码授予功能,因为我们不打算将其用作社交登录工具。但是,按照所有说明操作后,我在尝
我想设置事件菜单项的样式,为此我需要将当前 url 与路由进行比较。我知道我可以在 javascript 中做到这一点,但我想知道其他人是如何解决这个问题的。 有什么建议么? 伪代码: My Page
我正在尝试在浏览器上以图形方式显示路径/路线以供客户查看。例如,基于 txt 或 XML 文件,包含说明。 4 90 5 90 2 或 F4,L90,F5,L90,F2 相当于
我创建了一个中间件来阻止我的 laravel 应用程序中的某些路由,但不起作用,无法弄清楚我做错了什么,这是我的代码: ps:我使用的是 laravel 5.2 路线: Route::get('sec
我正在使用 Java 工作。给定一个矩阵 NxM,我需要找到通过该数组的所有可能路径。只允许斜向上或斜向下,或向右斜行。 4x4 矩阵示例: 3 5 7 9 2 4 6 8 9 3 7
我是 Marionette 新手,只是找不到上类路线。 我正在使用 Marionette 的 2.4.1 版本,并尝试以最简单的方式进行操作,以便它能够正常工作。 此代码适用于旧版本的 Marione
我是 AngularJS 的新手。我正在尝试从这个网站( https://docs.angularjs.org/tutorial/step_07 )学习 AngularJS。我的代码如下 index.
我在 yandexmapkit-android 项目上工作。图书馆链接是 https://github.com/yandexmobile/yandexmapkit-android 文档非常薄弱,git
我正在阅读有关 Angular 路由的文档并创建了一个简单的测试: const routes: Route[] = [ { path: '', redirectTo: '/home', pat
我正在开发一项服务 (spring-boot),它获取一个 ID 列表,一个一个地从数据库中获取对象,将这些对象聚合成批处理,然后将它们保存在其他地方。目前,聚合后的批量大小约为 50 个对象,大约每
我正在制作一个网站,在用户登录后,用户将被重定向到主页。网站的主页和所有其他页面只能由登录用户访问,但即使在用户登录后(firebase auth),网站的其余部分( protected 路由)仍然无
我有一个惰性模块,我希望在桌面和移动设备上有不同的体验。基本上我想要我的桌面布局如下: Component1 显示一个列表,用户在列表中选择一个项目,component2 将显示详细信息。我创建了名为
我是 Angular 的新手,我正在尝试让我的路由器工作。基本上我在 / 有一个主页,其中有一个到 /courses 的路由器链接,它运行良好,但是当我重新加载 /courses 时(或输入地址in)
完整的 Mojolicious 应用程序有 routes将转储应用程序路由的命令: script/my_app.pl routes 我如何从 Lite 的测试脚本中做同样的事情应用? use Mojo
我有一个 Camel 2.13.1 应用程序,它使用我通过 CXF 组件访问的外部 Web 服务。我使用 Spring XML 路由元素的 startupOrder 属性来确保在我设置为在启动时调用一
我们有一个在 Karaf 2.4.3 和 Camel 2.15.3 上运行的数据处理应用程序。 在这个应用程序中,我们有一堆导入数据的路由。我们有一个管理 View ,其中列出了这些路由以及每条路由的
我正在尝试组合一个应用程序,我可以在其中查询谷歌路线服务,存储结果以建立缓存,然后根据需要呈现路线。 我可以取回方向数据并将其存储在数据库中就好了,这一切都很好,现在当我在 map 上渲染方向时,我的
我根据 Ryan Bates 的 railscast 使用设计登录创建了一个新项目. 它没有注册路线(与我之前制作的项目不同,步骤完全相同) This image显示了两个“rake 路由”命令。顶
我发现 Google Maps API 通过以下方式支持路线: var map; var directionsPanel; var directions; function initialize()
我是一名优秀的程序员,十分优秀!