I was wondering what could be the best approach to access data on my setup function, from a composable method... If it is possible at all (and a good practice).
For example, on my main component, I would have a reactive data object with all of the variables used in that component... but not all of them will be used by my composable.
Here's an example of what I'm trying to do (It's kinda like a model composable)...
Let's say that I have a useDrivers composable, and want to use its saveDriver method, something like this:
// all shortened for readability
import useDrivers from "@composables/use-drivers";
export default {
setup() {
const { saveDriver } = useDrivers();
const data= reactive({
foo: null,
bar: null,
accident: { ... },
driver: { ... },
anotherUsedVar: null,
});
// instead of doing something like this:
saveDriver(driver, accident.id, anotherUsedVar);
// or even worse:
saveDriver(driver.name, driver.gender, driver.license, accident.id, anotherUserVar);
// Could I, somehow, access my DATA const from inside my method?
// but, again, without having to pass whatever amount of parameters
saveDriver();
}
}
@composables/use-drivers? The problem is very specific to it. If it's yours, you need to design API the way it's more comfortable to use. Here you likely need to pass a single "context" object that contains relevant data. Could be a computed if you need to keep it reactive within the hook. "Could I" - no, and even if you could, accessing arbitrary variables from another scope would be an awful thing, design-wise,computedapproach tomorrow. And yeah, I was kinda looking for the “could/should I” answer to know it if was a good practice or not.use-drivers, would be my API requests logic (get, store, update, delete), hypothetically speaking.