pinia 在VUE3+Vite+stuep语法糖中的使用

 推荐文章

Vue3 小菠萝 Pinia使用_vue3 pinia_我是ed.的博客-CSDN博客

1. 安装pinia

npm install pinia

2.mian.js中注册全局引用,挑选需要的使用

//mian.js
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
// pinia状态管理
import { createPinia } from 'pinia'
import "./assets/css/main.css"
// 引入Ant Design
import * as AntdIcon from "@ant-design/icons-vue"
import Antd from 'ant-design-vue'
import 'ant-design-vue/dist/antd.less'
// 引入axios
import axios from 'axios'
import VueAxios from 'vue-axios'
// 引入elementUI
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import MyComponent from './App.vue'

// 动态import基础组件
import Rzpop from './components/rzpop.vue'
import CloudPlay from './components/CloudPlay.vue'
import LatticeAssistant from './views/play/components/LatticeAssistant.vue'

const pinia = createPinia()
const app = createApp(App)


// 注册所有图标 Ant Design图标
for (const i in AntdIcon) {
  app.component(i, AntdIcon[i])
}
//注册所有图标 elementUI图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}



app.component(
  // 注册的名字
  'CloudPlay',
  // 组件的实现
  CloudPlay
)
app.component('Rzpop', Rzpop)
app.component('LatticeAssistant',LatticeAssistant)

app.use(pinia)
app.use(Antd)
app.use(router)
// 导入axios
app.use(VueAxios,axios)
// 导入elementUI
app.use(ElementPlus)

app.mount('#app')

3. 使用

 3.1 user.js

//user.js

import { defineStore } from "pinia"

export const useUserStore = defineStore('user', {
  state: () => {
    return {
      info: {}, // 用户信息
    }
  },
  getters: {},
  actions: {}
})

3.2 index.js---此份代码主要是pinia在mian.js全局注册前,注册全局组件准备的

//index.js
import { createPinia } from 'pinia';
const pinia = createPinia();

export default pinia;

4.添加数据

//user.js


import { getUser } from "../api/user"
import { localKey } from "./constant"
import { useUserStore } from "../store/user"
import { setUserInfo } from "./clientMethods"
// import store from '../store/index.js'

const userStore = useUserStore()//当前使用
// const userStore = useUserStore(store) //全局组件使用

/**
 * 获取用户信息
 */
export const getUserInfo = () => {
    if(localStorage.getItem(localKey.TOKEN)) {
        getUser().then(res => {
            userStore.info = res //这里获取并修改数据
            setUserInfo(JSON.stringify(res))
        })
    }else {
        console.log('用户未登录')
        setUserInfo('')
    }
}

5. 获取数据

        acb.vue

<script setup>
import { storeToRefs } from 'pinia'
import { useUserStore } from '@/store/user'

const userInfo = useUserStore()
const { info } = storeToRefs(userInfo)

console.log(info.value?.id)

</script>

<template>

    <div>
            {{info?.id}}
    </div>

</template>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

刘海583

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

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

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

打赏作者

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

抵扣说明:

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

余额充值