vue3 在 js 文件中使用 store 报错

背景:

页面的 vue 文件引入了 js 文件,这个 js 文件中是抽离的一些变量,用到了 store 

pinia 是在 main 文件中注册的

 

问题:

import { storeToRefs } from 'pinia'
import { xxStore } from '@/store/xx'

const XxStore = xxStore()
const { xx } = storeToRefs(XxStore)

export default () => {
    return {
        ...
        xx    
    }
}

Uncaught Error: [🍍]: getActivePinia was called with no active Pinia. Did you forget to install pinia?  

 

解决:

调整使用的时机

import { storeToRefs } from 'pinia'
import { xxStore } from '@/store/xx'

export default () => {
    const XxStore = xxStore()
    const { xx } = storeToRefs(XxStore)

    return {
        // ...
        xx    
    }
}

 

posted @ 2023-02-16 16:56  yuhui_yin  阅读(599)  评论(0编辑  收藏  举报