chore: adjust useRoute

This commit is contained in:
chenjiahan 2020-09-26 11:17:49 +08:00
parent 367a273bd5
commit 12cadc42a1
2 changed files with 14 additions and 12 deletions

View File

@ -1,8 +1,14 @@
/** /**
* Vue Router support * Vue Router support
*/ */
import { getCurrentInstance } from 'vue'; import { getCurrentInstance, ComponentPublicInstance } from 'vue';
import type { Router, RouteLocation } from 'vue-router'; import type { RouteLocation } from 'vue-router';
export type RouteProps = {
url?: string;
replace?: boolean;
to?: RouteLocation;
};
export type RouteConfig = { export type RouteConfig = {
url?: string; url?: string;
@ -18,8 +24,10 @@ function isRedundantNavigation(err: Error) {
); );
} }
export function route(router: Router, props: RouteProps) { export function route(vm: ComponentPublicInstance<RouteProps>) {
const { to, url, replace } = props; const router = vm.$router;
const { to, url, replace } = vm;
if (to && router) { if (to && router) {
const promise = router[replace ? 'replace' : 'push'](to); const promise = router[replace ? 'replace' : 'push'](to);
@ -39,16 +47,10 @@ export function route(router: Router, props: RouteProps) {
export function useRoute() { export function useRoute() {
const vm = getCurrentInstance()!.proxy!; const vm = getCurrentInstance()!.proxy!;
return () => { return () => {
route(vm.$router, vm as RouteProps); route(vm);
}; };
} }
export type RouteProps = {
url?: string;
replace?: boolean;
to?: RouteLocation;
};
export const routeProps = { export const routeProps = {
url: String, url: String,
replace: Boolean, replace: Boolean,

View File

@ -273,7 +273,7 @@ export default createComponent({
}); });
emit('click', name, title); emit('click', name, title);
route(item.$router, item); route(item);
} }
}; };