refactor(projects): 路由声明重构,添加composables,BaseLayout进行中,文件夹规范
This commit is contained in:
63
src/composables/common/route.ts
Normal file
63
src/composables/common/route.ts
Normal file
@ -0,0 +1,63 @@
|
||||
import { computed, watch } from 'vue';
|
||||
import { useRoute } from 'vue-router';
|
||||
import { routeName } from '@/router';
|
||||
import type { RouteKey } from '@/interface';
|
||||
|
||||
/**
|
||||
* 路由属性
|
||||
* @description - 必须要在setup里面调用
|
||||
*/
|
||||
export function useRouteProps() {
|
||||
const route = useRoute();
|
||||
const props = computed(() => {
|
||||
/** 路由名称 */
|
||||
const name = route.name as string;
|
||||
/** 缓存页面 */
|
||||
const keepAlive = Boolean(route.meta?.keepAlive);
|
||||
/** 视高100% */
|
||||
const fullPage = Boolean(route.meta?.fullPage);
|
||||
|
||||
return {
|
||||
name,
|
||||
keepAlive,
|
||||
fullPage
|
||||
};
|
||||
});
|
||||
|
||||
return props;
|
||||
}
|
||||
|
||||
/**
|
||||
* 路由查询参数
|
||||
* @description - 必须要在setup里面调用
|
||||
*/
|
||||
export function useRouteQuery() {
|
||||
const route = useRoute();
|
||||
|
||||
/** 登录跳转链接 */
|
||||
const loginRedirectUrl = computed(() => {
|
||||
let url: string | undefined;
|
||||
if (route.name === routeName('login')) {
|
||||
url = (route.query?.redirectUrl as string) || '';
|
||||
}
|
||||
return url;
|
||||
});
|
||||
|
||||
return {
|
||||
loginRedirectUrl
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 路由名称变化后的回调
|
||||
* @param callback
|
||||
*/
|
||||
export function routeNameWatcher(callback: (name: RouteKey) => void) {
|
||||
const route = useRoute();
|
||||
watch(
|
||||
() => route.name,
|
||||
newValue => {
|
||||
callback(newValue as RouteKey);
|
||||
}
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user