refactor: 重构项目的数据获取方法 #72
@ -1,4 +1,4 @@
|
||||
import { readItem } from '@directus/sdk';
|
||||
import GetSolution from '@/graphql/solution.graphql?raw';
|
||||
|
||||
export const useSolution = (id: string) => {
|
||||
const { $directus } = useNuxtApp();
|
||||
@ -6,23 +6,9 @@ export const useSolution = (id: string) => {
|
||||
const locale = getDirectusLocale();
|
||||
|
||||
return useAsyncData(`solution-${id}-${locale}`, async () => {
|
||||
return await $directus.request(
|
||||
readItem('solutions', id, {
|
||||
fields: [
|
||||
'id',
|
||||
{
|
||||
translations: ['*'],
|
||||
},
|
||||
'create_at',
|
||||
],
|
||||
deep: {
|
||||
translations: {
|
||||
_filter: {
|
||||
languages_code: { _eq: locale },
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
);
|
||||
return await $directus.query<{ solutions_by_id: Solution }>(GetSolution, {
|
||||
id: id,
|
||||
locale: locale,
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
12
app/graphql/solution.graphql
Normal file
12
app/graphql/solution.graphql
Normal file
@ -0,0 +1,12 @@
|
||||
query GetSolution($id: ID!, $locale: String!) {
|
||||
solutions_by_id(id: $id) {
|
||||
id
|
||||
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
|
||||
id
|
||||
title
|
||||
summary
|
||||
content
|
||||
}
|
||||
create_at
|
||||
}
|
||||
}
|
||||
@ -26,16 +26,16 @@
|
||||
const route = useRoute();
|
||||
const localePath = useLocalePath();
|
||||
|
||||
// 获取路由参数(documentId)
|
||||
const id = computed(() => route.params.slug as string);
|
||||
// 获取路由参数
|
||||
const id = route.params.slug as string;
|
||||
|
||||
const { data, pending, error } = await useSolution(id.value);
|
||||
const { data, pending, error } = await useSolution(id);
|
||||
|
||||
const solution = computed(() => {
|
||||
if (!data.value) {
|
||||
return null;
|
||||
}
|
||||
return toSolutionView(data.value);
|
||||
return toSolutionView(data.value.solutions_by_id);
|
||||
});
|
||||
|
||||
const breadcrumbItems = computed(() => [
|
||||
Reference in New Issue
Block a user