chore: remove useless navigation duplicated

This commit is contained in:
chenjiahan 2020-09-26 11:23:13 +08:00
parent 12cadc42a1
commit 7bf95d4bcf

View File

@ -1,44 +1,28 @@
/**
* Vue Router support
*/
import { getCurrentInstance, ComponentPublicInstance } from 'vue';
import {
PropType,
ExtractPropTypes,
getCurrentInstance,
ComponentPublicInstance,
} from 'vue';
import type { RouteLocation } from 'vue-router';
export type RouteProps = {
url?: string;
replace?: boolean;
to?: RouteLocation;
export const routeProps = {
to: [String, Object] as PropType<RouteLocation>,
url: String,
replace: Boolean,
};
export type RouteConfig = {
url?: string;
to?: RouteLocation;
replace?: boolean;
};
function isRedundantNavigation(err: Error) {
return (
err.name === 'NavigationDuplicated' ||
// compatible with vue-router@3.3
(err.message && err.message.indexOf('redundant navigation') !== -1)
);
}
export type RouteProps = ExtractPropTypes<typeof routeProps>;
export function route(vm: ComponentPublicInstance<RouteProps>) {
const router = vm.$router;
const { to, url, replace } = vm;
if (to && router) {
const promise = router[replace ? 'replace' : 'push'](to);
/* istanbul ignore else */
if (promise && promise.catch) {
promise.catch((err) => {
if (err && !isRedundantNavigation(err)) {
throw err;
}
});
}
router[replace ? 'replace' : 'push'](to);
} else if (url) {
replace ? location.replace(url) : (location.href = url);
}
@ -50,9 +34,3 @@ export function useRoute() {
route(vm);
};
}
export const routeProps = {
url: String,
replace: Boolean,
to: [String, Object],
};