uni报错:getActivePinia was called with no active Pinia. Did you forget to install pinia?
时间: 2023-12-13 17:22:36 浏览: 208
14.(vue3.x+vite)组件间通信方式之pinia.rar
该错误是由于在使用pinia之前没有正确安装和初始化pinia库引起的。在使用pinia之前,需要在main.ts文件中进行初始化。以下是正确的初始化步骤:
1. 在main.ts文件中导入createPinia函数和App组件:
```javascript
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
```
2. 使用createPinia函数创建一个pinia实例并将其注册到应用程序中:
```javascript
const pinia = createPinia()
const app = createApp(App)
app.use(pinia)
```
3. 在其他地方使用pinia时,确保在创建store之前调用`app.use(pinia)`。例如,在project-pinia.ts文件中:
```javascript
export const useProjectStore = defineStore('project', {
state: () => ({ appName:"", lang:"", loginUser:{} })
})
const store = useProjectStore()
```
如果在调用`app.use(pinia)`之前尝试使用pinia,就会遇到"getActivePinia was called with no active Pinia. Did you forget to install pinia?"错误。因此,请确保在使用pinia之前正确安装和初始化pinia库。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
阅读全文