Using Vue.js web components with Pinia
P粉755863750
P粉755863750 2023-12-20 10:36:14
0
1
506

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粉755863750
P粉755863750

reply all(1)
P粉832212776

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()
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!