问题已更新
我尝试使用 Pinia 的商店以及使用 Vue.js 创建的 Web 组件,但控制台中出现此错误:
[Vue warn]:在
处找不到注入“Symbol(pinia)”
我有一个非常简单的例子。
import { defineCustomElement } from 'vue'
import HelloWorld from './components/HelloWorld.ce.vue'
const ExampleElement = defineCustomElement(HelloWorld)
customElements.define('hello-world', ExampleElement)
import { defineStore, createPinia, setActivePinia } from "pinia";
setActivePinia(createPinia());
export const useCounterStore = defineStore('counter', {
state: () => ({
counter: 0,
}),
actions: {
increment() {
this.counter++;
},
},
});
<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> Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
在 main.js 中创建 pinia 后,您将在商店中重新创建 pinia。从您的商店中删除这些行:
import { createPinia } from 'pinia' const pinia = createPinia()