Skip to content

Commit 4460603

Browse files
authored
queriesにリアクティブな値が設定できない不具合を修正 (#10)
1 parent 9b61ed2 commit 4460603

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,37 @@ const { data: blogs } = await useMicroCMSGetList<Blog>({
128128
</script>
129129
```
130130

131+
#### Use reactive values for query parameters
132+
133+
```vue
134+
<template>
135+
<input type="text" v-model="queries.q" />
136+
</template>
137+
138+
<script setup lang="ts">
139+
import type { MicroCMSImage } from 'microcms-js-sdk';
140+
141+
type Blog = {
142+
title: string;
143+
eyecatch: MicroCMSImage;
144+
};
145+
146+
const queries = reactive({
147+
q: '',
148+
fields: ['id', 'title', 'eyecatch'],
149+
});
150+
151+
const { data: blogs, refresh } = await useMicroCMSGetList<Blog>({
152+
endpoint: 'blogs',
153+
queries,
154+
});
155+
156+
watch(queries, () => {
157+
refresh();
158+
});
159+
</script>
160+
```
161+
131162
# LICENSE
132163

133164
Apache-2.0

src/runtime/composables/useMicroCMSGet.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from 'microcms-js-sdk';
77
import { useFetch, useRuntimeConfig } from 'nuxt/app';
88
import type { FetchOptions as _FetchOptions } from 'ofetch';
9+
import { computed } from 'vue';
910

1011
import { useMicroCMSUrl } from './useMicroCMSUrl';
1112

@@ -41,13 +42,15 @@ const useMicroCMSGet = <T>(
4142
const baseURL = useMicroCMSUrl();
4243
const config = useRuntimeConfig();
4344

44-
const query: MicroCMSQueries | undefined = queries
45-
? {
46-
...queries,
47-
fields: queries.fields?.toString(),
48-
ids: queries.ids?.toString(),
49-
}
50-
: undefined;
45+
const query = computed<MicroCMSQueries | undefined>(() =>
46+
queries
47+
? {
48+
...queries,
49+
fields: queries.fields?.toString(),
50+
ids: queries.ids?.toString(),
51+
}
52+
: undefined
53+
);
5154

5255
return useFetch<T>(url, {
5356
...fetchOptions,

0 commit comments

Comments
 (0)