Home  >  Q&A  >  body text

Achieve enhanced composition and responsiveness with Vue.js 3.

<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粉935883292P粉935883292442 days ago560

reply all(2)I'll reply

  • P粉136356287

    P粉1363562872023-08-04 12:13:23

    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


    reply
    0
  • P粉786800174

    P粉7868001742023-08-04 11:18:17

    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

    reply
    0
  • Cancelreply