Skip to content

How to use ...useState within <script setup> ? #24

Open
@Fenrir200678

Description

@Fenrir200678

Hi,
I'm currently playing around with the vuex composition helpers. It works fine if you use it in the setup method like in the docs:

import { useVuex } from '@vueblocks/vue-use-vuex'

export default {
  setup () {
    const { useState } = useVuex()

    return {
      ...useState({
        count: state => state.count,
      })
    }
  }
}

But how can we get it to work within <script setup>, since you can't use the spread operator outside of an object / array?

<script setup>
import { useVuex } from '@vueblocks/vue-use-vuex'

const { useState } = useVuex()
...useState({    // Won't work
  count: state => state.count
})
</script>

I tried to assign it to a variable like so

const { useState } = useVuex()
const store = {
  ...useState({
    count: state => state.count
  })
}
const { count } = store

But that throws an Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'state').
What am I missing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions