单击后退按钮后如何获取以前的URL并导航他?

时间:2017-06-19 12:35:46

标签: angular

我在组件的构造函数中使用它:

  this.router.events
    .filter(e => e instanceof NavigationEnd)
    .pairwise().subscribe((e) => {
        this.previousUrl = e[0].url;
    });

现在我有同样的组成部分:

  returnToPreviousPage(){
        console.log(this.previousUrl);
        // this.router.navigate([this.previousUrl]);
    }

但我总是未定义。任何建议我如何使用前一个网址以及当用户点击返回按钮导航他

 this.previousUrl

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式转到上一页:

import { Location } from '@angular/common';

@Component ({
    // ...
})

export class MyComponent {

    constructor(private location: Location) { }

    goBack(): void {
        this.location.back();
    }  
}
相关问题