Achieve enhanced composition and responsiveness with Vue.js 3.
P粉935883292
P粉935883292 2023-08-03 15:58:35
0
2
606
<p>Given the following combined 'useNumbers.js' in Vue.js 3, I want 'numbers' in the component to be responsive. However, adding a new value to 'numbers' in one component will not be reflected in the second component. <br /><br />useNumbers.js:</p><p><br /></p> <pre class="brush:php;toolbar:false;">import { ref } from 'vue'; export default function() { const numbers = ref([1, 2, 3, 4, 5]); const addNumber = num => { numbers.value.push(num); } const filterNumber = minNum => { return numbers.value.filter(curNum => curNum >= minNum); } return { numbers, addNumber, filterNumber } }</pre> <p>Component A:</p> <pre class="brush:php;toolbar:false;"><template> <div> <h1>Child component</h1> <p>{{ numbers }}</p> <button @click="addNumber(45)">Add 45</button> <div class="line"></div> <GrandChild /> </div> </template> <script setup> import useNumbers from '../composables/useNumbers'; import GrandChild from './GrandChild.vue'; const { numbers, addNumber } = useNumbers() </script></pre> <p>Component B:</p> <pre class="brush:php;toolbar:false;"><template> <div> <h1>GreatGrandChild component</h1> <p>{{ numbers }}</p> <div class="line"></div> </div> </template> <script setup> import useNumbers from '../composables/useNumbers'; const { numbers } = useNumbers(); </script></pre> <p> Whenever I click the button in component A (adding 45 to the array), this change is not reflected in component B. I know that operations like destructuring assignment result in loss of responsiveness. </p> <pre class="brush:php;toolbar:false;">const { numbers, addNumber } = useNumbers()</pre> <p>Broken responsiveness. I've tried a few things using toRef and toRefs but haven't found a solution yet. </p>
P粉935883292
P粉935883292

reply all(2)
P粉136356287

The main reason for your problem is that every time the useNumbers function is called, a new numbers object instance is created. Therefore, each component has its own copy of numbers, which are not shared between components.

Use store


P粉786800174

To maintain the same state across multiple useNumbers instances, you can promote the state outside the function so that it is only created once.

import { ref } from 'vue';

const numbers = ref([1, 2, 3, 4, 5]);

export default function() { 
  ...
  return { numbers }
}

Vue Playground example

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template