【发布时间】:2019-05-12 15:38:09
【问题描述】:
我创建了一个小服务,我可以在其中存储路线更改。
import { Injectable } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
@Injectable()
export class RouteState {
private previousUrl: string;
private currentUrl: string;
constructor(private router: Router) {
this.currentUrl = this.router.url;
router.events.subscribe(event => {
if (event instanceof NavigationEnd) {
this.previousUrl = this.currentUrl;
this.currentUrl = event.url;
};
});
}
public getPreviousUrl() {
return this.previousUrl;
}
}
但每次路由更改时,vars currentUrl 和 previousUrl 都是未定义的。我做错了吗?
【问题讨论】:
-
你添加到 app.module 提供者列表了吗? , 一切正常????????
-
您的代码在 RouteState 服务中按您的意愿工作,我认为您在调用 getPreviousUrl 函数时或在其他地方犯了错误。如果提供您的代码,那么我可以帮助您。
-
问题是每次路由改变时,previousUrl都是未定义的。我没有保存价值。
标签: angular typescript angular7