在Angular 2中有没有聪明的方法返回到最后一页?
类似的
this._router.navigate(LASTPAGE);
例如,C页有一个“返回”按钮,
A页-> C页,点击返回A页。 B页-> C页,点击它,回到B页。
路由器有这个历史信息吗?
在Angular 2中有没有聪明的方法返回到最后一页?
类似的
this._router.navigate(LASTPAGE);
例如,C页有一个“返回”按钮,
A页-> C页,点击返回A页。 B页-> C页,点击它,回到B页。
路由器有这个历史信息吗?
当前回答
RC4:
import {Location} from '@angular/common';
其他回答
此外,您还可以在历史记录为空的情况下使用此服务与回退功能
url-back.service.ts
import { Injectable } from '@angular/core';
import { Location } from '@angular/common';
import { Router } from '@angular/router';
const EMPTY_HISTORY_LENGTH = 2;
/**
* This service helps to Navigate back to the prev page, and if no prev page,
* will redirect to the fallback url.
*/
@Injectable()
export class UrlBackService {
constructor(private router: Router, private location: Location) {}
/**
* This method will back you to the previous page,
* if no previous page exists, will redirect you to the fallback url.
* @param href - url, if tryNativeFirst is provided, this is fallback url
* @param tryNativeFirst - try to go back natively using browser history state.
*/
back(href: string, tryNativeFirst: boolean = false) {
if (tryNativeFirst) {
if (history.length === EMPTY_HISTORY_LENGTH) {
this.router.navigate(UrlBackService.urlToArray(href));
} else {
this.location.back();
}
} else {
this.router.navigate(UrlBackService.urlToArray(href));
}
}
/**
* In case that router.navigate method tries to escape all '/' in the string,
* was decided to split string to array, and if URL starts or ends with slash - remove them, eg:
* /my/url will be split to ['', 'my', 'url'], so we need to remove empty spaces use filter function.
* @param href
* @private
*/
private static urlToArray(href: string) {
return href.split('/').filter((notEmpty) => notEmpty);
}
}
url-back.service.spec.ts
import { TestBed } from '@angular/core/testing';
import { UrlBackService } from './url-back.service';
import { Router } from '@angular/router';
import { Location } from '@angular/common';
import { RouterTestingModule } from '@angular/router/testing';
describe('UrlBackService', () => {
let service: UrlBackService;
let router: Router;
let location: Location;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule],
providers: [UrlBackService],
});
service = TestBed.inject(UrlBackService);
router = TestBed.inject(Router);
location = TestBed.inject(Location);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
it('no meter what history state is, it should be redirected to the /my/url', () => {
spyOn(router, 'navigate');
service.back('/my/url');
expect(router.navigate).toHaveBeenCalledWith(['my', 'url']);
});
it('in case history is empty push to /my/url', () => {
spyOn(router, 'navigate');
service.back('/my/url', true);
expect(router.navigate).toHaveBeenCalledWith(['my', 'url']);
});
it('in case history is NOT empty push to url1', () => {
spyOn(location, 'back');
window.history.pushState(null, '', 'url1');
service.back('/my/url', true);
expect(location.back).toHaveBeenCalled();
});
});
2022 利用你的应用程序路由-更多的是一种“角度方法”,而不是访问浏览器的位置对象来获取导航历史。 想想你为什么需要用户返回,以及“返回”在应用程序及其路由的更广泛的上下文中意味着什么。
例如,从子路由返回到父路由
this.router.navigate(['..'], {relativeTo: this.route});
您还可以阅读以前的导航
previousNavigation:先前成功的导航对象。只有 一个先前的导航是可用的,因此这个先前的 Navigation对象的previousNavigation为空值。
RC4:
import {Location} from '@angular/common';
另一个解决方案
window.history.back ();
你可以在你的路由类上实现routerOnActivate()方法,它会提供之前路由的信息。
routerOnActivate(nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction) : any
然后你可以使用router.navigateByUrl()并传递ComponentInstruction生成的数据。例如:
this._router.navigateByUrl(prevInstruction.urlPath);