I try the new composition api.
composable function useProdcut.js:
export default function useProduct() {
const product = reactive({});
async function getProduct(id) {
try{
const response = await {{http Request}}
product.value = response.data; // it is a single object
}catch(err) {
console.log(err);
}
}
return {product, getProduct}
}
In my component I use two composable functions:
setup(_, {root}) {
…
const { product, getProduct} = useProduct();
const {productAlias, productTitle, createdAt, …} = useProductDetails(prodcut);
onBeforeMount(async () => {
loading.value = true;
await getProduct(root.$route.params.id);
loading.value = false;
});
return {loading, product, productAlias, …}
}
In useProductDetails.js I got the message:
product.value is undefined.
I think this is because of the value for product is retrieved in onBeforeMount. But how can I achieve to pass the product ref with value after mounted? Or is there an other way?
I appreciate any help. Thank you!