34 lines
903 B
TypeScript
34 lines
903 B
TypeScript
import { watch } from 'vue'
|
|
import { useRoute, useRouter } from 'vue-router'
|
|
|
|
/**
|
|
* 监听 route.query.editId,调用 openById;结束后清除 editId,避免刷新重复弹窗。
|
|
* openById 应自行处理加载失败提示。
|
|
*/
|
|
export function useRouteEditIdOpen(openById: (id: string) => void | Promise<void>) {
|
|
const route = useRoute()
|
|
const router = useRouter()
|
|
|
|
async function consumeEditId() {
|
|
const raw = route.query.editId
|
|
const id = Array.isArray(raw) ? String(raw[0] || '') : String(raw || '')
|
|
if (!id.trim()) return
|
|
try {
|
|
await openById(id.trim())
|
|
} finally {
|
|
if (route.query.editId == null) return
|
|
const query = { ...route.query }
|
|
delete query.editId
|
|
await router.replace({ path: route.path, query })
|
|
}
|
|
}
|
|
|
|
watch(
|
|
() => route.query.editId,
|
|
() => {
|
|
void consumeEditId()
|
|
},
|
|
{ immediate: true }
|
|
)
|
|
}
|