Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access composable state from a module?

I am trying to pass state value of number from one store to another, why it isn't working? Getting number is not defined.

import {
  reactive,
  toRefs
} from 'vue';
  
const state = reactive({
  number: 12
})

export function test() {
  
  return {
    ...toRefs(state)
  }
}

to this:

import {
  reactive,
  toRefs,
} from 'vue';

import { test } from '@/myvuex/somestore';

const state = reactive({
  testValue: 5
})

export function useGlobal() {

  console.log(number)

  return {
    ...toRefs(state),
    test,
    number
  }
}
like image 746
Dave Avatar asked May 11 '26 04:05

Dave


1 Answers

The test import is a function, so it has to be called. It returns an object containing the refs of the first module's state.

Then you can pass on the object's number property:

const importedState = test();         // retrieving the state from module #1
const number = importedState.number;  // the `number` ref
    
export function useGlobal() {
  console.log(number.value);          // It's a ref now, use `.value`
      
  return {
    ...toRefs(state),
    test,
    number
  }
}
like image 200
Dan Avatar answered May 12 '26 17:05

Dan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!