Hello,
I'm learning Vue and I don't understand why passing an array property into a ref() doesn't seem to work.
Here is an example to illustrate my question :
Shopping.vue
<template>
<h3>Choose fruits:</h3>
<ul>
<li v-for="fruit in fruits" :key="fruit.name">
<input
type="checkbox"
:id="fruit.name"
v-model="selectedFruits"
:value="fruit"
>
<label :for="fruit.name">
{{ fruit.name }} (from {{ fruit.country }})
</label>
</li>
</ul>
<hr>
<Cart :products="selectedFruits" />
</template>
<script setup>
import { ref } from "vue";
import Cart from "@/Components/Cart";
const fruits = [
{name: 'Apple', country: 'Canada'},
{name: 'Avocado', country: 'Mexico'},
{name: 'Strawberry', country: 'Spain'},
];
const selectedFruits = ref([]);
</script>
Cart.vue
<template>
<h3>In your cart:</h3>
<ul>
<li v-for="product in products">
{{ product.name }} from ({{ product.country }})
</li>
</ul>
</template>
<script setup>
import { ref } from "vue";
const props = defineProps({
products: Array
});
const products = ref(props.products);
</script>
Because I'm doing const products = ref(props.products), my cart list doesn't list the products. If I directly loop on my prop (without creating a const products) it works. Why?
Thank you!