用VUE选项API设置基本PINIA商店

发布于 2025-01-24 16:48:15 字数 1058 浏览 0 评论 0原文

我是Pinia的新手,并且很难仅设置基本商店。我一直在关注Pinia自己的文档,并且似乎无法从我映射到Pinia商店的Vue组件中读取任何状态。

在我的应用程序中,我有:

import { createPinia } from 'pinia';
export default function initApp(el) {
    let app = createApp(MenuApp);
    app.use(router).use(createPinia()).mount(el);
}

我设置了一个超基本的Pinia商店,只是为了开始:

import {defineStore} from 'pinia';

export const useTestPiniaStore = defineStore('testStore', {
    state: () => {
        return {
            name: 'bob'
        }
    },
})

在我的Vue组件中,我拥有的:

<template>
    <div class="menu-page">
        <h1>{{name}}</h1>
    </div>
</template>
<script>
import { mapState } from 'pinia';
import useTestPiniaStore from '@store/modules/piniaStore';

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

Pinia出现在我的Vue Dev工具中,但其中没有商店出现,我会收到错误 无法读取未定义的属性(读取“名称”)

我不明白我在这里做错了什么。如果有人能提出一些可以如此欣赏的指针。

I am new to Pinia, and am having trouble setting up just a basic store. I have been following Pinia's own documentation, and cannot seem to read any state whatsoever from the vue component I'm mapping to the Pinia store.

In my app I have:

import { createPinia } from 'pinia';
export default function initApp(el) {
    let app = createApp(MenuApp);
    app.use(router).use(createPinia()).mount(el);
}

I set up a super basic Pinia store, just to get started:

import {defineStore} from 'pinia';

export const useTestPiniaStore = defineStore('testStore', {
    state: () => {
        return {
            name: 'bob'
        }
    },
})

In my vue component I have:

<template>
    <div class="menu-page">
        <h1>{{name}}</h1>
    </div>
</template>
<script>
import { mapState } from 'pinia';
import useTestPiniaStore from '@store/modules/piniaStore';

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

Pinia appears in my Vue dev tools, but no stores appear within it, and I get the error
Cannot read properties of undefined (reading 'name')

I don't understand what I am doing wrong here. If anyone can give some pointers that would be so appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的呼吸 2025-01-31 16:48:15

mapState() 两个参数,但您只通过了一个。

第一个参数应为usetestpiniastore,第二个参数应为映射的状态属性数组(或对象)。看来您正在尝试从usetestpiniastore引用name,这将是undefined

您的计算道具应该看起来像这样:

<script>
import { mapState } from 'pinia'
import { useTestPiniaStore } from '@/store'

export default {
computed: {
...mapState(useTestPiniaStore, ['name']),

mapState() requires two arguments, but you've passed it only one.

The 1st argument should be useTestPiniaStore, and the 2nd should be an array of state properties to map (or an object). It looks like you're trying to reference name from useTestPiniaStore, which would be undefined.

Your computed prop should look like this:

<script>
import { mapState } from 'pinia'
import { useTestPiniaStore } from '@/store'

export default {
  computed: {
    ...mapState(useTestPiniaStore, ['name']), ????
  },
}
</script>

demo

栀梦 2025-01-31 16:48:15

当我试图使用PINIA和VUE使用选项API设置MapState时,我也没有运气。

对我有用的是使用Mapstores。这是 cookbook :

import { mapStores } from 'pinia'

// given two stores with the following ids
const useUserStore = defineStore('user', {
  // ...
})
const useCartStore = defineStore('cart', {
  // ...
})

export default {
  computed: {
    // note we are not passing an array, just one store after the other
    // each store will be accessible as its id + 'Store'
    ...mapStores(useCartStore, useUserStore)
  },

  methods: {
    async buyStuff() {
      // use them anywhere!
      if (this.userStore.isAuthenticated()) {
        await this.cartStore.buy()
        this.$router.push('/purchased')
      }
    },
  },
}

I didn't have luck with mapState as well when I was trying to set it up with both Pinia and Vue using Options API.

What worked for me was using mapStores. Here's an example from the cookbook:

import { mapStores } from 'pinia'

// given two stores with the following ids
const useUserStore = defineStore('user', {
  // ...
})
const useCartStore = defineStore('cart', {
  // ...
})

export default {
  computed: {
    // note we are not passing an array, just one store after the other
    // each store will be accessible as its id + 'Store'
    ...mapStores(useCartStore, useUserStore)
  },

  methods: {
    async buyStuff() {
      // use them anywhere!
      if (this.userStore.isAuthenticated()) {
        await this.cartStore.buy()
        this.$router.push('/purchased')
      }
    },
  },
}
情释 2025-01-31 16:48:15

我在VUE3上,我的商店在PINIA上(设置方法)。我有一些基于选项API的组件,我想在Pinia Store中使用功能(操作)和数据值。
Dominik-Serafin的建议和按照厨师书的建议cookbook/options-api.html 使用Mapstores满足我的要求。

因为我的pinia有一家带有id“ ix”的商店
在我的组件中...

...
import { mapStores } from 'pinia'
import { useixStore } from '@/stores/ix'
...
export default {
    props: ...
    data...
    computed: {
        ...mapStores( useixStore ),
        ...

根据菜谱,“默认情况下,pinia将在每个商店的ID中添加“存储”后缀。您可以通过调用setMapStoresuffix()来自定义此行为。”

因此,现在我可以使用this.ixstore.xyz在组件脚本中访问整个商店,并在组件模板中使用ixstore.xyz ...。

I am on Vue3 and my stores is on Pinia (setup method). I have a few components that are based on Options API and I wanted to use the functions (actions) and data values in the Pinia store.
Suggestion of dominik-serafin and as per cook book https://pinia.vuejs.org/cookbook/options-api.html using mapStores meets my requirement.

for eg my Pinia has a Store with id 'ix'
in my component...

...
import { mapStores } from 'pinia'
import { useixStore } from '@/stores/ix'
...
export default {
    props: ...
    data...
    computed: {
        ...mapStores( useixStore ),
        ...

As per cookbook "By default, Pinia will add the "Store" suffix to the id of each store. You can customize this behavior by calling the setMapStoreSuffix()."

So now I am able to access my entire store using this.ixStore.xyz... in component script and by using ixStore.xyz... in component template.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文