返回Angular 4中的上一页

Ela*_*ene 5 angular-ui-router angular

我想要回到上一页.

上一页: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));
    }
}
Run Code Online (Sandbox Code Playgroud)

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

<a routerLink="">Back</a>
Run Code Online (Sandbox Code Playgroud)

Ela*_*ene 18

这项工作由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
  }
}
Run Code Online (Sandbox Code Playgroud)

HTML

<a (click)="cancel()">Back</a>
Run Code Online (Sandbox Code Playgroud)

  • 您能否为您的评论@ mast3rd3mon提供更多详细信息? (4认同)