Pinia入门使用(简易模板)

目录

一、概念

二、配置(具体参照官方文档)

1、使用create-vue创建空的新项目

2、根据文档安装pinia到目录

3、在main.js中加入代码


一、概念

Pinia是Vue专属的最新的状态管理库,是vuex状态管理工具的替代品

1、提供更简单的API(去掉了Mutation)

2、提供符合组合式风格的API(和Vue3统一)

3、去掉了modules概念,每一个store都是一个独立的模块

4、搭配TS一起使用提供可靠的类型判断

二、配置(具体参照官方文档)

1、使用create-vue创建空的新项目

npm init vue@latest

2、根据文档安装pinia到目录

npm install pinia

3、在main.js中加入代码
import { createPinia } from 'pinia'
const pinia = createPinia()
createApp(App).use(pinia).mount('#app')

三、基本使用(以计数器为例)

在src/stores/counter.js中
import { defineStore, storeToRefs } from 'pinia'
import { computed, ref } from 'vue'
// useStore 可以是 useUser、useCart 之类的任何东西
// 第一个参数是应用程序中 store 的唯一 id
export const useCounterStore = defineStore('counter',()=>{
    // 定义数据State
    const count = ref(0)
    // 定义方法action=同步+异步
    const add = ()=>{
        count.value++
    }
    // 异步action
    const list = ref([]) 
    const getList = async()=>{
        const res = await axios.get(API_URL)
        list.value = res.data.data.channels
    }

    // getter
    const doubleCount = computed(()=>count.value*2)
    
    // 以对象方式return
    return{
        count,
        add,
        doubleCount,
        list,
        getList
    }
})
在要使用的组件中
<script setup>
// 导入use开头的方法
import { useCounterStore } from './stores/counter';
// 执行得到store实例对象
const counterStore= useCounterStore()
console.log(counterStore);
</script>

<template>
  <div>{{ counterStore.count }}</div>
  <button @click="counterStore.add">+1</button>
</template>

<style scoped>
</style>
2、getter的实现

Pinia中getters直接使用computed进行模拟

    // getter
    const doubleCount = computed(()=>count.value*2)
3、action实现异步

和组件里定义数据风格一致

    // 异步action
    const list = ref([]) 
    const getList = async()=>{
        const res = await axios.get(API_URL)
        list.value = res.data.data.channels
    }

记得把该return的return出去

4、storeToRefs

使用storeToRefs函数可以辅助保持数据(state+getter)的响应式解构

错误案例(错误的,就放个图了):

<script setup>
// 导入use开头的方法
import { useCounterStore } from './stores/counter';
import { storeToRefs } from 'pinia';
// 执行得到store实例对象
const counterStore= useCounterStore()
console.log(counterStore);
    // 解构同时保证不丢失响应式
    const {count,doubleCount} = storeToRefs(counterStore)
</script>

<template>
  <div>{{ counterStore.count }}</div>
  <div>{{ counterStore.doubleCount }}</div>
  <div>{{ doubleCount }}</div>
  <div>{{ count }}</div>
  <button @click="counterStore.add">+1</button>

</template>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Pinia 是一个由 Vue.js 社区维护的状态管理库,它是为 Vue 3 设计的。下面是使用 Pinia 的一般步骤: 1. 首先,你需要安装 Pinia。可以通过 npm 或 yarn 进行安装: ```shell npm install pinia ``` 2. 在你的应用程序的入口文件中,导入 Pinia 并创建一个新的 Pinia 实例: ```javascript import { createApp } from 'vue' import { createPinia } from 'pinia' import App from './App.vue' const app = createApp(App) const pinia = createPinia() app.use(pinia) app.mount('#app') ``` 3. 在你的应用程序中定义一个或多个 store。一个 store 是一个包含状态和操作的对象。 ```javascript // store.js import { defineStore } from 'pinia' export const useCounterStore = defineStore('counter', { state() { return { count: 0 } }, actions: { increment() { this.count++ } } }) ``` 4. 在你的组件中使用 store。可以使用 `useStore` 函数从 Pinia 实例中获取 store 的实例: ```javascript import { defineComponent } from 'vue' import { useCounterStore } from './store' export default defineComponent({ setup() { const counterStore = useCounterStore() function increment() { counterStore.increment() } return { counterStore, increment } } }) ``` 5. 在模板使用 store 的状态和操作: ```html <template> <div> <p>Count: {{ counterStore.count }}</p> <button @click="increment">Increment</button> </div> </template> ``` 这是一个简单的示例,演示了如何在 Vue 3 中使用 Pinia 进行状态管理。你可以根据你的应用程序的需求定义更多的 store 并在组件中使用它们。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值