使用Vue.js Web组件与Pinia
P粉755863750
P粉755863750 2023-12-20 10:36:14
0
1
504

问题已更新

我尝试使用 Pinia 的商店以及使用 Vue.js 创建的 Web 组件,但控制台中出现此错误:

[Vue warn]:在 处找不到注入“Symbol(pinia)”

我有一个非常简单的例子。

  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

全部回复(1)
P粉832212776

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

import { createPinia } from 'pinia'
const pinia = createPinia()
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!