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, '').replace('//', '/'); 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) { if (!this.enter()) { return; } const { url, props } = this.getUrlAndProps(options, state, disableNamespace); this.history.push(url, props); this.leave(); this.publish(); } async redirectTo(options, state, disableNamespace) { if (!this.enter()) { return; } const { url, props } = this.getUrlAndProps(options, state, disableNamespace); this.history.replace(url, props); this.leave(); this.publish(); } async switchTab(options, state, disableNamespace) { console.error('浏览器无switchTab'); const { url, props } = this.getUrlAndProps(options, state, disableNamespace); this.history.replace(url, props); this.publish(); } async navigateBack(delta) { if (!this.enter()) { return; } this.history.go(delta ? 0 - delta : -1); this.leave(); this.publish(); } navigateBackOrRedirectTo(options, state, disableNamespace) { console.error('浏览器暂无法获得history堆栈'); this.history.back(); this.publish(); } }