目前的文档只讨论获取路由参数,而不是实际的路径段.
例如,如果我想找到当前路线的父母,那怎么可能呢?
我期待检测我的路线变化AppComponent.
此后,我将检查全局用户令牌以查看他是否已登录.然后,如果用户未登录,我可以重定向用户.
我已经构建了一个router 3.0.0-beta.1用于在应用程序部分之间切换的应用程序 我还location.go()用来模拟同一页面的子部分之间的切换.我使用<base href="/">了一些URL重写规则,以便index.html在页面刷新的情况下重定向所有路由.这允许路由器接收所请求的子部分作为URL参数.基本上我设法避免使用HashLocationStrategy.
routes.ts
export const routes: RouterConfig = [
{
path: '',
redirectTo: '/catalog',
pathMatch: 'full'
},
{
path: 'catalog',
component: CatalogComponent
},
{
path: 'catalog/:topCategory',
component: CatalogComponent
},
{
path: 'summary',
component: SummaryComponent
}
];
Run Code Online (Sandbox Code Playgroud)
如果我点击导航栏中的子部分,则会发生以下情况:
logation.go() 使用必要的字符串更新URL以指示当前子节scrollTo()动画会在请求的子部分顶部滚动页面.如果我刷新页面,我使用先前定义的路线并提取必要的参数以恢复滚动到所请求的子部分.
this._activatedRoute.params
.map(params => params['topCategory'])
.subscribe(topCategory => {
if (typeof topCategory !== 'undefined' &&
topCategory !== null
) {
self.UiState.startArrowWasDismised = true;
self.UiState.selectedTopCategory = topCategory;
} …Run Code Online (Sandbox Code Playgroud) 这是我目前使用旧库的尝试.
我现在正在使用最新的库,我将如何更新:
import { Component, } from '@angular/core';
import { Location } from '@angular/common';
import { Router } from '@angular/router';
@Component({
moduleId: module.id,
selector: 'HeaderComponent',
templateUrl: 'header.component.html'
})
export class HeaderComponent{
router : Router;
constructor(router: Router, location: Location){
this.router = router;
this.router.changes.subscribe((currentRoute) => {
let currentRoute = this.location.path();
})
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的模块:
export * from './header.component';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { HeaderComponent } from './header.component'; …Run Code Online (Sandbox Code Playgroud) 如何获取订阅的router.events内部的活动路由数据(如params)?
我也没有想订阅this.route.params!
// Catch any events in a certain component, where I subscribe to it!
this.router.events.subscribe(event =>
{
// The event object has only the url (when is NavigationStart/End), there is nothing useful for me
});
Run Code Online (Sandbox Code Playgroud)
更新
我的问题有所不同,因为我不问如何对路由器事件做出反应!
我要求如何在router.events中获取激活的路由参数!
那肯定是有区别的!
如何访问路由开始和路由结束事件?我想在路由和加载数据时显示进度条。
有人可以告诉我这是如何工作的吗?