oak-frontend-base/es/features/navigator.web.js

63 lines
2.1 KiB
JavaScript

import { createBrowserHistory } from 'history';
import { Navigator as CommonNavigator } from './navigator.common';
export class Navigator extends CommonNavigator {
history;
constructor() {
super();
this.history = createBrowserHistory();
}
/**
* 必须使用这个方法注入history才能和react-router兼容
* @param history
*/
setHistory(history) {
this.history = history;
}
getLocation() {
const { pathname } = this.history.location;
const url = pathname.replace(this.namespace, '');
return {
...this.history.location,
namespace: this.namespace,
url: url || '/',
};
}
getState() {
const { pathname, state, search } = this.getLocation();
const state2 = this.constructState(pathname, state, search);
return state2;
}
getUrlAndProps(options, state, disableNamespace) {
const { url, ...rest } = options;
const url2 = this.constructUrl(url, rest, disableNamespace);
const { pathname } = this.getLocation();
const state2 = Object.assign({}, state, {
oakFrom: pathname,
});
return {
url: url2,
props: state2,
};
}
async navigateTo(options, state, disableNamespace) {
const { url, props } = this.getUrlAndProps(options, state, disableNamespace);
this.history.push(url, props);
}
async redirectTo(options, state, disableNamespace) {
const { url, props } = this.getUrlAndProps(options, state, disableNamespace);
this.history.replace(url, props);
}
async switchTab(options, state, disableNamespace) {
console.error('浏览器无switchTab');
const { url, props } = this.getUrlAndProps(options, state, disableNamespace);
this.history.replace(url, props);
}
async navigateBack(delta) {
this.history.go(delta ? 0 - delta : -1);
}
navigateBackOrRedirectTo(options, state, disableNamespace) {
console.error('浏览器暂无法获得history堆栈');
this.history.back();
}
}