Comments (4)
Not really sure what you are trying to do, why not just do this?
// useDocuments.ts
export const useDocuments = async () => {
const { data: documents, ...rest } = await useLarafetch("/documents", {
lazy: true,
});
return {
documents,
...rest,
};
};
// documents.vue
<script setup lang="ts">
const {documents, pending} = await useDocuments();
</script>
<template>
<div>
<div v-if="pending">Loading...</div>
<pre v-else>{{ documents }}</pre>
</div>
</template>
from breeze-nuxt.
I want to save the documents and function in composable to use in different components as state. I.e I need to reload documents when user add new document in createDocument.vue component or when edit the title in editDocument component. Also I have a paginated documents. I think to call to getDocuments with the new cursor to push new documents in infinite scroll
from breeze-nuxt.
In this case I would suggest using pinia and calling $larafetch
directly, maybe something like this:
// useDocuments.ts
import { defineStore } from "pinia";
export const useDocuments = defineStore("documents", () => {
const documents = ref<undefined | null | any[]>(undefined);
let page = ref(1);
const pending = ref(false);
async function getDocuments() {
pending.value = true;
documents.value = documents.value ?? null;
const newDocs = await $larafetch(`/documents?page=${page.value}`);
pending.value = false;
documents.value = [...(documents.value || []), ...newDocs];
}
async function nextPage() {
page.value++;
return getDocuments();
}
return {
documents,
page,
nextPage,
pending,
};
});
// documents.vue
<script setup lang="ts">
const docStore = useDocuments();
if (docStore.documents === undefined) {
await docStore.nextPage();
}
</script>
<template>
<div>
<pre v-if="docStore.documents">{{ docStore.documents }}</pre>
<div v-if="docStore.pending">Loading...</div>
<button @click="() => docStore.nextPage()">Get</button>
</div>
</template>
Hope this helps you out,
I am closing this since it is not directly related to this repo.
from breeze-nuxt.
BTW if you are looking for a full-featured solution to this kind of problem then you might want to consider using TanStack Query, they have an example of making it work with Nuxt.
from breeze-nuxt.
Related Issues (20)
- 419 CSRF token mismatch. HOT 2
- crf token issue HOT 1
- Fresh install, upgrade nuxt to latest, get infinite loops on /register and /forgot-password HOT 2
- How to use $larafetch in server? HOT 6
- It would be cool if you can make this a nuxt module HOT 4
- initCsrf is always called HOT 2
- The guest middleware not working with pinia stores HOT 29
- Redirecting to Login Page on Refresh HOT 2
- [nuxt] [request error] [unhandled] [500] [GET] "http://localhost:8000/api/user": <no response> fetch failed HOT 1
- Error 500 A composable that requires access to the nuxt instance.... HOT 6
- Being redirected to login page on refresh HOT 8
- Duplicated XSRF TOKEN with custom subdomains HOT 4
- useEvent is not defined HOT 1
- npm run build error [nuxt] [request error] [unhandled] [500] useEvent is not defined HOT 6
- Modify schema of login/register/password-reset etc. response
- Only works on same domain HOT 1
- ohmyfetch import HOT 1
- Blob via submitRequest util HOT 9
- Preloader when refreshing page HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from breeze-nuxt.