vue3 pinia踩坑

安装

yarn add pinia # or npm install pinia复制代码

用法


// user.ts
import { defineStore } from 'pinia'
export const useUserStore = defineStore({
  id: 'user',
  state: () => ({
    ...
  }),
  actions: {
    ...
  }
})

// components.vue
import { useUserStore } from '@/store/user'
const userStore = useUserStore()

使用中出现的问题

1、Cannot access 'useUserStore' before initialization

重现步骤

通常我们会在路由钩子中判断用户是否登陆,从而进行权限判断。如:


// permission.ts
import { NavigationGuardNext, RouteLocationNormalized } from 'vue-router'
import router from './router'

import { useUserStore } from './store/user'
const userStore: any = useUserStore()

router.beforeEach(async(to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
  // TODO 判断是否登录
  if (userStore.name) {
    ...
  }
})

// main.ts
const app = createApp(App)
app.use(createPinia())
import router from './router'
import '@/permission'
app.use(router)
app.mount('#app')

问题原因

代码从上往下执行过程中,遇到const userStore: any = useUserStore(),会进行用户状态模块的获取,但是应用还没有挂载,所以Pinia的全局状态还没有进行初始化。造成了初始化用户模块状态获取时,全局状态并没有初始化,所以造成了当前问题。

解决方案

在路由钩子函数进行用户状态模块的获取,调用路由钩子的时候,意味着全局状态已完全初始化完成。但会造成每次调用路由钩子都获取用户状态模块,会造成资源的浪费(当然可以达到预期目的,但并不是我们需要的)。我们可以在外层声明一个变量用来存储状态,在路由钩子中进行判断,如果当前变量为空,也就意味着状态还没有进行过获取,在当前情况下进行状态获取(类似于单例)。最终代码:


// permission.ts
import { NavigationGuardNext, RouteLocationNormalized } from 'vue-router'
import router from './router'

import { useUserStore } from './store/user'
let userStore: any = null

router.beforeEach(async(to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
  if (userStore === null) {
    userStore = useUserStore()
  } 
  // TODO 判断是否登录
  if (userStore.name) {
    ...
  }
})
Vue3 Pinia是一个基于Vue3的状态管理库,它提供了类似于Vuex的全局状态管理能力,同时使用起来更加简单方便。下面是Vue3 Pinia的使用步骤: 1. 安装Vue3 Pinia 你可以使用npm或者yarn安装Vue3 Pinia: ``` npm install pinia ``` 或者 ``` yarn add pinia ``` 2. 创建Pinia Store 你需要创建一个store来管理你的状态。一个store就是一个拥有状态和操作状态的对象。下面是一个例子: ``` import { defineStore } from &#39;pinia&#39; export const useCounterStore = defineStore({ id: &#39;counter&#39;, state: () => ({ count: 0 }), actions: { increment() { this.count++ } } }) ``` 这个例子中,我们创建了一个名为useCounterStore的store,它拥有一个状态count,初始值为0,并且有一个操作increment,用来增加count的值。 3.Vue组件中使用Store 你可以在Vue组件中使用createPinia函数创建一个Pinia实例,并通过inject注入到组件中。然后就可以通过store来获取状态和操作状态了。下面是一个例子: ``` <template> <div> <p>{{ count }}</p> <button @click="increment">+1</button> </div> </template> <script> import { defineComponent, inject } from &#39;vue&#39; import { useCounterStore } from &#39;./store&#39; export default defineComponent({ setup() { const store = useCounterStore() const count = computed(() => store.count) const increment = () => { store.increment() } return { count, increment } } }) </script> ``` 这个例子中,我们通过useCounterStore函数来获取useCounterStore实例,并且通过computed函数来获取count的值。然后我们在increment函数中调用store的increment方法。 4. 相关问题: 1. Vue3 PiniaVuex有什么区别? 2. 如何在多个组件之间共享状态? 3. Vue3 Pinia如何处理异步操作? 4. Vue3 Pinia如何处理模块化?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑狼传说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值