【问题标题】:Vuex not working in boiler plate Nuxt appVuex 在样板 Nuxt 应用程序中不起作用
【发布时间】:2024-04-29 14:55:03
【问题描述】:

我无法让 Vuex 开箱即用地与 Nuxt 一起工作。我确实尝试按照本指南进行操作,但出现以下错误

我得到的错误:

TypeError
Cannot read property 'state' of undefined

重现步骤:

npx create-nuxt-app

\\Options selected

? Project name **VueNuxtApp**
? Project description **My epic Nuxt.js project**
? Use a custom server framework **none**
? Choose features to install (Press <space> to select, <a> to toggle all, <i> to invert selection)
? Use a custom UI framework **none**
? Use a custom test framework **none**
? Choose rendering mode **Universal**
? Author name
? Choose a package manager **npm**

npm run dev

pages/index.vue 替换为以下内容:

<template>
  <div>
    <h3>The website says</h3>
    <p>{{ greeting }}</p>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  computed: mapState([
    'greeting'
  ]),
}
</script>

添加了store/index.js

export const state = () => ({
  greeting: 'Hello, World'
})

【问题讨论】:

  • 您在其他地方有错误。您的代码正在运行,并且您的问题无法通过您描述的步骤重现
  • 感谢您的帮助,Aldarund。你能在这个例子中让它工作吗?我试了几次都没有运气。
  • 是的,我是。为您的问题创建一个代码和博克或 GitHub 存储库。
  • 好的。似乎需要重新启动才能完成这项工作。我不确定这是否是 nuxt 中的热重载不支持的东西。
  • 即使没有热重载也应该可以工作

标签: vue.js axios vuex nuxt.js


【解决方案1】:

我想通了。需要重新启动以反映更改。不过,我在官方指南中没有看到提及这一点。

【讨论】: