八、Vue3基础之八


一、Pinia.js

Pinia.js有如下特点:

  1. 完整的ts支持
  2. 足够轻量,压缩后的体积只有1kb左右
  3. 去除mulations,只有state,getters,actions
  4. actions支持同步和异步
  5. 代码扁平化没有模块嵌套,只有store的概念,store之间可以自由使用,每一个store都是独立的
  6. 无需手动添加store,store一旦创建会自动添加
  7. 支持vue3和vue2

安装pinia.js

npm install pinia --save

引入pinia.js
main.ts

import { createApp } from 'vue'
import App from './App.vue'
// 使用Pinia --
import { createPinia } from 'pinia'
const store = createPinia()
// 使用Pinia --
let app = createApp(App)

// 使用Pinia --
app.use(store)
// 使用Pinia --
app.mount('#app')

初始化仓库Store
在store下创建store-name.ts

export const enum Names { // 用来做为store的key
    TEST = 'TEST'
}

在store下创建index.ts

import { defineStore} from 'pinia'
import { Names } from './store-name'

export const useTestStore = defineStore(Names.TEST, {
    state: () => {  //初始、需要用到的数据都定义到这里来
        return {
            name: '张三',
            age: 14
        }
    },
    // computed修饰一些值
    getters:{

    },
    // methods 可以做同步,异步,提交state
    actions: {

    }

})

在App.vue中使用这个store

<template>

    <div>
      Pinia:{{Test.name}}--{{Test.age}}
    </div>

</template>

<script setup lang='ts'>
import { useTestStore } from './store'
const Test = useTestStore()

</script>

<style lang='scss' scoped>

</style>

1.1 State

state的值是允许修改的,有五种方式可以修改state的值

<template>

    <div>
      Pinia:{{Test.name}}--{{Test.age}}  <br>
      change:
      <button @click="change">change</button>
    </div>

</template>

<script setup lang='ts'>
import { useTestStore } from './store'
const Test = useTestStore()

const change = () =>{
    // Test.name = '王五'; 第一种修改的方式

    // Test.$patch({
    //   name: '赵六'
    // }) 第二种修改的方式
    
    // 第三种修改方式
    // Test.$patch((state)=>{ //这里的参数就是state
    //     state.age = 20
    // })

    // 第四种修改方式,state对象中的数据全都要改不然出错
    // Test.$state = {
    //   name: '赵六',
    //   age: 30
    // }

    // 第五种方式使用store中的actions方法来修改
    Test.setAge() 

}

</script>

<style lang='scss' scoped>

</style>

在这里插入图片描述

1.2 解构store

App.vue

<template>

    <div>
      Pinia:{{Test.name}}--{{Test.age}}  <br>
      解构出来的值{{name}}--{{age}} <br>
      change:
      <button @click="change">change</button>
    </div>

</template>

<script setup lang='ts'>
import { useTestStore } from './store'
import { storeToRefs } from 'pinia';
const Test = useTestStore()
// 解构出来不具有响应式,所以需要加上storeToRefs
const {name, age} = storeToRefs(Test)

const change = () =>{
    age.value++
    name.value= '五五'
}

</script>

<style lang='scss' scoped>

</style>

1.3 Actions

store下的index.ts

import { defineStore} from 'pinia'
import { Names } from './store-name'

type User = {
    name: string
    age: number
}

const result:User = {
    name: '张三',
    age: 30
}

const Login = ():Promise<User> => {
    return new Promise((resolve)=>{
        setTimeout(()=>{
            resolve({
                name: "嘎子哥",
                age: 40
            })
        }, 2000)
    })
}

export const useTestStore = defineStore(Names.TEST, {
    state: () => {  //初始、需要用到的数据都定义到这里来
        return {
            user: <User>{},
            hobby: '打球'
        }
    },
    // computed修饰一些值
    getters:{
        // 有缓存
        getUerName():string{
            return `$-${this.user.name}`
        }
    },
    // methods 可以做同步,异步,提交state
    actions: {
        // 同步方法
        // setUser(){
        //     this.user = result
        // }

        // 异步方法
        async setUser(){
            const login = await Login()
            this.user = login
        }
    }

})

App.vue

<template>

    <div>
      pinia: {{Test.user.name}} -- {{Test.user.age}} <br>
      getter: {{Test.getUerName}}
      <button @click="change">change</button>
    </div>

</template>

<script setup lang='ts'>
import { useTestStore } from './store'
const Test = useTestStore()

const change = () =>{
    Test.setUser()
}

</script>

<style lang='scss' scoped>

</style>

1.3 pinia持久化

pinia和vuex页面刷新状态会丢失。
可以写一个pinia插件缓存他的值。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

永恒的宁静

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

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

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

打赏作者

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

抵扣说明:

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

余额充值