pinia中ts this指向错误?

import { defineStore } from 'pinia';

const useTestStore = defineStore('test',{
    state: () => ({
        counter: 0,
    }),
    actions: {
        increment() {
            this.counter++
        },
        randomizeCounter() {
            this.counter = Math.round(100 * Math.random())
        },
    },
})

export default useTestStore

我在ts + vue3的项目中如上照抄了pinia官网的示例,运行的时候this会报错指向actions
image.png
请问该如何解决

阅读 4.5k
2 个回答

有没有先 useTestStore()?大概如下:

import useTestStore from './store';

const testStore = useTestStore();
testStore.increment();
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题