angular - 返回 Angular 4 中的上一页

标签 angular angular-ui-router

<分区>

我想回到上一页。

上一页:job.html 当前页面:jobDetail.html

按照说明,我已将 import { Location } from '@angular/common'; 添加到顶部的 jobDetail.component.ts 文件中,然后是

export class MyDetailComponent implements OnInit {
constructor(private location: Location) {}
    ngOnInit() {
        this.location.subscribe(x => console.log(x));
    }
}

我在 jobDetail.html 中有一个 html 代码,但不知道如何继续。如何正确添加上一个按钮。对于像我这样的新手,没有简单的教程。

<a routerLink="">Back</a>

最佳答案

这是由 Hinrich 发布的:

import { Location } from '@angular/common';
// more imports here...

@Component({
  // component's declarations here
})
export class MyComponent {

  constructor(private location: Location) { } // inject Location into class constructor

  cancel() {
    this.location.back(); // <-- go back to previous location on cancel
  }
}

HTML

<a (click)="cancel()">Back</a>

关于angular - 返回 Angular 4 中的上一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47428640/

相关文章:

angular - 仅在更改 ngx-currency 后选择值

javascript - Angular 将样式标签添加到 innerHTML

javascript - Angular Ui 路由器 |从 Controller 访问 View 中的范围变量,但不起作用

javascript - Angular UI 路由器 : URL changed but view isn't loaded

angularjs - Angular HTML5Mode - 重写规则

javascript - 用户界面路由器 : async data in templateUrl function

html - Angular1 : TypeError: promise. catch 不是函数

angular - 停止 formcontrol 和 formgroup 的值更改

typescript - 如何使用 angular2 从 MockBackend 获取 header

angular - 我得到的只是键而不是 AngularFire2 : Realtime Database version 5 using Angular 6. 0 的值