1

I get this working in the Vue 3 with the composition API if I use the regular setup() method like so:

<script>
  import { useStore } from 'vuex'
  import { computed } from 'vue'

  export default {
    const store = useStore()

    setup () {
      const loginStatus = computed(() => store.getters.loginStatus)

      return { loginStatus }
    }
  }
</script>

loginStatus is now exposed to the template and can be used in the html.

But when I try the newer <script setup> syntax the const isn't picked up and exposed to the template anymore.

I'm not supposed to return anything from script setup, but it doesn't happen automatically either. Eslint marks loginStatus as unused. I can't find any information about how Vuex is supposed to be used in this context:

<script setup>
  import { useStore } from 'vuex'
  import { computed } from 'vue'
  const store = useStore()

  const loginStatus = computed(() => store.getters.loginStatus)
</script>

Is this expected not to work? Or do you know the recommended way of doing it?

EDIT 1:

I'm aware of this answer where the accepted solution isn't recommended composition API syntax and the second answer involves writing self-created boilerplate which makes what I want to do possible, but doesn't seem like the official way (if there is one).

EDIT 2:

As pointed out by commenters, my code actually did work. I was however fooled by the Vetur extension marking those unreturned variables as unused. So it looked to me like this was the reason it wasn't picked up in the template. In reality an unrelated error was the real reason.

Because of this Vetur issue I'll stick with the older setup syntax for now.

3
  • 1
    Your <script setup> should work. If it doesn't work for you, please share a reproduction of the problem. Commented Aug 22, 2021 at 1:27
  • You are right, it actually does work. But Vetur fooled me, it shows unreturned variables as unused, so it looked to me that was the reason it wasn't picked up in the template. But it was an unrelated error that was the real reason. Because of this Vetur problem I'll stick with the older setup syntax for now. Commented Aug 22, 2021 at 3:41
  • 1
    Try to use the vuedx extension marketplace.visualstudio.com/… Commented Aug 22, 2021 at 7:18

3 Answers 3

3

I kept trying and

import { computed } from 'vue';
import { useStore } from 'vuex';

const store = useStore();

const con = computed(() => store.getters.connected);

In this case connected is just a boolean but I also tried it with objects and it still works.

My template:

<template>
  <h1>{{ con }}</h1>
</template>

And it works just fine.

Sign up to request clarification or add additional context in comments.

1 Comment

You are right, my code actually did work. But Vetur fooled me, it shows those unreturned variables as unused, so it looked to me like that was the reason it wasn't picked up in the template. But it was an unrelated error that was the real reason. Because of this Vetur problem I'll stick with the older setup syntax for now. Thanks for investigating!
2

Your usage in <script setup> actually works, but as pointed out in comments, the Vetur VS Code extension was showing misleading errors.

As of 0.34.1, Vetur does not support <script setup>. The recommended extension for <script setup> is Volar. This was even tweeted yesterday from Vue's official Twitter account:

tweet

Comments

0
import { computed } from 'vue';
import { useStore } from 'vuex';

const store = useStore();

const con = computed(() => store.getters['moduleName/connected'];

1 Comment

Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.