vuejs3 - 在 nuxt 3 中设置 pinia 商店时遇到困难

标签 vuejs3 nuxtjs3 pinia

我目前正在尝试使用 nuxt 3 和 pinia 来设置一个项目进行状态管理,但遇到了以下错误:

[h3] [unhandled] H3Error: defineStore is not defined
    at createError (file:///home/johnr/Code/Personal/test/node_modules/h3/dist/index.mjs:191:15)
    at Server.nodeHandler (file:///home/johnr/Code/Personal/test/node_modules/h3/dist/index.mjs:381:21) {
  statusCode: 500,
  fatal: false,
  unhandled: true,
  statusMessage: 'Internal Server Error'
}

我使用 npx nuxi init 初始化了项目,然后运行 ​​npm i,然后运行 ​​npm install @pinia/nuxt。然后我将 pinia 添加到 nuxt.config.ts 中:

 // nuxt.config.js
    export default {
        // ... other options
        modules: [
            // ...
            '@pinia/nuxt',
        ],
    }

并在 store/counter.js 中创建了一个基本商店:

 export const useCounterStore = defineStore('counter', () => {
  const count = ref(0);
  function increment() {
    count.value++;
  }

  return { count, increment };
});

并尝试在应用模板中使用返回的计数:

<template>
  <div>
    <p>The count is {{ counterStore.count.value }}</p>
  </div>
</template>

<script setup>
import { useCounterStore } from './store/counter.js';
const counterStore = useCounterStore();
</script>

最佳答案

您似乎忘记在 store/counter.js 中导入 defineStore:

从“pinia”导入{defineStore}

关于vuejs3 - 在 nuxt 3 中设置 pinia 商店时遇到困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73124132/

相关文章:

vue.js - 如何在 Nuxt 3 中启用reactivityTransform?

twitter-bootstrap - 如何将 Bootstrap5 与 Vite 和 Nuxt3 一起使用?

typescript - Pinia 存储与可组合函数

typescript - 明确输入商店中的属性?

vue.js - pinia 在 Action 中使用 state 而不是 this

vue.js - 在 VueJS 中使用另一个组件内的组件

javascript - 从参数化的 getter 访问 getter

vue.js - Vue3 中未发出 beforeDestroy 生命周期钩子(Hook)

javascript - 当页面重新加载时正在监视的属性未更改时,Vue watch 不会运行