search

Home  >  Q&A  >  body text

Using Vue.js web components with Pinia

Question has been updated

I tried using a store from Pinia and a web component created with Vue.js but I got this error in the console:

[Vue warn]: Injection "Symbol(pinia)" not found at

I have a very simple example.

  1. main.ts
import { defineCustomElement } from 'vue'
import HelloWorld from './components/HelloWorld.ce.vue'

const ExampleElement = defineCustomElement(HelloWorld)
customElements.define('hello-world', ExampleElement)
  1. store.ts
import { defineStore, createPinia, setActivePinia } from "pinia";

setActivePinia(createPinia());

export const useCounterStore = defineStore('counter', {
  state: () => ({
    counter: 0,
  }),

  actions: {
    increment() {
      this.counter++;
    },
  },
});
  1. HelloWorld.ce.vue
<script setup lang="ts">
import { ref } from 'vue'
import { useCounterStore } from '../store.ts'

defineProps<{ msg: string }>()

const store = useCounterStore()
</script>

<template>
  <h1>{{ msg }}</h1>
  <div class="card">
    <button type="button" @click="store.increment()">count is {{ store.counter }}</button>
  </div>
</template>

P粉755863750P粉755863750348 days ago532

reply all(1)I'll reply

  • P粉832212776

    P粉8322127762023-12-21 00:07:25

    After creating pinia in main.js, you will recreate pinia in the store. Remove these lines from your store:

    import { createPinia } from 'pinia'
    const pinia = createPinia()

    reply
    0
  • Cancelreply