how to call actions from pinia store?

Viewed 3469

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");
}
2 Answers

You need to create a store instance and call actions

 const store = useLessonsNStore()

 store. openLessonCard()

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

Related