安全中国asp.net网站开发项目实战培训班,北京百姓网免费发布信息网,三亚百度推广公司,培训体系在Angular中#xff0c;有多种方法可以实现返回上一页。
使用 location.back() Location服务提供了back方法#xff0c;该方法用于导航到浏览器历史记录的上一页。
import { Location } from angular/common;// 在构造函数中注入 Location
constructor(private location: L…在Angular中有多种方法可以实现返回上一页。
使用 location.back() Location服务提供了back方法该方法用于导航到浏览器历史记录的上一页。
import { Location } from angular/common;// 在构造函数中注入 Location
constructor(private location: Location) {}// 返回上一页
goBack() {this.location.back();
}使用 Router 服务 Router服务提供了多种导航方法其中之一是navigateBack可以用来返回上一页。
import { Router } from angular/router;// 在构造函数中注入 Router
constructor(private router: Router) {}// 返回上一页
goBack() {this.router.navigateBack([/]);
}使用 window.history 你还可以直接使用window.history对象来返回上一页。
import { Component } from angular/core;Component({selector: app-example,template: button (click)goBack()Go Back/button,
})
export class ExampleComponent {// 返回上一页goBack() {window.history.back();}
}选择其中一种方法根据你的项目需求和代码结构来决定使用哪种方式。