A
Alokai2y ago
Pavel

Composable

How to use methods from one composable (for example method clear in useCart) in another composable (useUser)?
4 Replies
skirianov
skirianov2y ago
following the usual Vue pattern should work
import { ref } from 'vue';
import useFirstComposable from './useFirstComposable';

export default function useSecondComposable() {
const { count, increment, doubledCount } = useFirstComposable();
const message = ref('Hello from the second composable!');

const resetCount = () => {
count.value = 0;
};

return {
message,
count,
increment,
doubledCount,
resetCount
};
}
import { ref } from 'vue';
import useFirstComposable from './useFirstComposable';

export default function useSecondComposable() {
const { count, increment, doubledCount } = useFirstComposable();
const message = ref('Hello from the second composable!');

const resetCount = () => {
count.value = 0;
};

return {
message,
count,
increment,
doubledCount,
resetCount
};
}
generally, I would not recommend it, in case if a method involves fetching but possible
Pavel
PavelOP2y ago
@skirianov Thanks for your reply, but when I'am trying to use one composable in another I got an error.
skirianov
skirianov2y ago
@Pavel I was expecting something like this tbh -_- Yeah, you can't call the composable like this, this is the limitation of composition api package I suppose. You will have to do it one by one in the setup method
Pavel
PavelOP2y ago
Got it, thanks!
Want results from more Discord servers?
Add your server