how to call actions from pinia store?
Asked Answered
V

2

7

How I can call action from pinia store in vue 3 component? I use composition API. For example, I would like to import 'openLessonCard' - it is action in pinia store. But this method for import don't work..

<script setup>
import { openLessonCard } from '../../stores/lessonsN.js';
import { ref, computed } from 'vue'


defineProps({
  data: {
    type: Object,
    required: true,
  },
  nocollapse: {
  type: Boolean,
  },
});

const reviewLevel = computed(() => {
  return Object.values(this.data.criteria).filter((i) => i === true).length;
})

async function editReviews(id, text, a, b, c, d, e) {
  let review = await this.$api.call("reviews.edit", {
    id,
    text,
    a,
    b,
    c,
    d,
    e,
  });
  this.$Message("ok");
}
Valvulitis answered 27/4, 2022 at 15:37 Comment(2)
Action is called from the store instance. So you need to get the store instance first. Please follow this guilde. If you still can not figure out what is the problem, please show the lessonsN.js's codeReconnaissance
in the example you attached above you are not using the action at all, where and how do you want to use it?Ayeshaayin
B
6

You need to create a store instance and call actions

 const store = useLessonsNStore()

 store.openLessonCard()
Bream answered 10/5, 2022 at 1:44 Comment(0)
R
3

Remember, "Actions are the equivalent of methods in components" (Pinia documentation).

You are using <script setup> in your example code.

Then, in your component...

  1. Import your store normally:
import { useLessonsNStore } from '../../stores/lessonsN';
  1. Get an instance of your store:
const lessonsN = useLessonsNStore();
  1. Invoke the action like a method
lessonsN.openLessonCard();

More info: enter link description here

Roslynrosmarin answered 10/7, 2022 at 4:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.