Verwendung von Vue.js-Webkomponenten mit Pinia
P粉755863750
P粉755863750 2023-12-20 10:36:14
0
1
496

Frage aktualisiert

Ich habe versucht, den Store von Pinia mit einer Webkomponente zu verwenden, die mit Vue.js erstellt wurde, aber ich habe diesen Fehler in der Konsole erhalten:

[Vue warnen]: Injektion „Symbol(pinia)“ nicht gefunden bei

Ich habe ein sehr einfaches Beispiel.

  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

Antworte allen(1)
P粉832212776

在 main.js 中创建 pinia 后,您将在商店中重新创建 pinia。从您的商店中删除这些行:

import { createPinia } from 'pinia'
const pinia = createPinia()
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!