我正在将一个角应用程序从ui-router升级到@angular/router (v6)。
有一个函数想要找到前一条路线。下面是使用来自Transition的ui-router/core的实现
const newFromName = this.transition.from().name;
this.transition.params('from')我已经删除了大部分函数,因为它不应该相关。
我的问题是:
是否有相当于上述两行的
@angular/router?
我们使用
"@uirouter/angular-hybrid": "3.1.7"
"@angular/router": "6.1.1",
注:这与动画无关。我不想激活一个过渡,我只想知道之前的路线是什么。
添加说明:例如,在导航到此组件(例如hello/world )之前,如果我在另一条路径上导航,然后使用my/new/route中的代码导航到路由。我需要知道/hello/world值
发布于 2019-05-07 13:58:30
您可以使用这个,并且在// Exit Code中您可以访问您来自的路由。
import { Router, NavigationStart, NavigationEnd } from '@angular/router';..。
constructor(router:Router) {
router.events.subscribe(e => {
if(e instanceof NavigationStart) {
// Init Code
}
if(e instanceof NavigationEnd) {
// Exit Code
}
}
});发布于 2019-05-07 15:10:57
方法1:创建服务
@Injectable()
export class RouterState {
private routeHistory = [];
constructor(private router: Router) {
// nothing to do
}
public startHistory() {
this.router.events
.pipe(filter(e => e instanceof NavigationEnd))
.subscribe(newUrl => this.routeHistory = [...this.routeHistory, newUrl]);
}
public getRouteHistory() {
return this.routeHistory;
}
}在应用程序组件中:
constructor(private routerState: RouterState) {
this.routerState.startHistory();
}在您需要获得历史记录的组件中:
constructor(private routerState: RouterState) {}
yourFunction() {
const allRoutes = this.routerState.getRouteHistory();
const lastUrl = !!allRoutes.length
? allRoutes.slice(-2)
: '';
}https://stackoverflow.com/questions/56023830
复制相似问题