使用Vue.js Web元件與Pinia
P粉755863750
P粉755863750 2023-12-20 10:36:14
0
1
505

問題已更新

#

我嘗試使用 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學習者快速成長!