如何回到 Ionic 4

Val*_*eri 6 javascript typescript angular ionic4

我想在 Ionic4(Angular 7)中添加一个后退按钮。但是我在 Angular Router 中找不到合适的方法。

import {Router} from '@angular/router';

在组件处理程序中单击按钮时我们如何返回?我想使用“@angular/router”而不是“@angular/common”=>位置来实现它

Pet*_*dad 6

由于您使用 ionic 4 然后倒退,您可以执行以下操作:

constructor(private navCtrl: NavController) {}

btnClick(){
this.navCtrl.navigateBack('/home'); 
} 
Run Code Online (Sandbox Code Playgroud)

  • 当 OP 使用角度路由时,使用 `@angular/common` 中的 `location.back()` 不是更合适吗?(见 https://angular.io/api/common/Location) (2认同)
  • 我刚刚意识到,当您直接使用 `location.back()` 时,您无法获得正确的后退动画。 (2认同)

小智 5

通过 Angular 路由,您可以使用 Location API:

constructor(private location: Location){}
Run Code Online (Sandbox Code Playgroud)

然后当您需要导航回调用时:

this.location.back();
Run Code Online (Sandbox Code Playgroud)

请记住,对于 Angular 7,您必须从以下位置导入位置:@angular/common