Pinia的基本用法

Pinia的安装和引入

1.安装Pinia

npm install pinia

2. 在vue项目的main.js文件中引入pinia

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'

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

app.use(pinia)
app.mount('#app')

Pinia的基本使用

使用pinia实现计数器

1.在src目录下创建stores目录,并新建文件counter.js
在这里插入图片描述
2. 在counter.js文件中使用defineStore定义对象useCounterStore

import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useCounterStore = defineStore('counter', () => {
    // 定义数据(state)
    const count = ref(0)

    // 定义修改数据的方法(action 同步+异步)
    const increment = () => {
        count.value++
    }

    // 以对象的方式return供组件使用
    return {
        count,
        increment
    }
})

3.在App.vue文件中导入counter.js文件中的useCounterStore

<script setup>
// 1. 导入use打头的方法
import { useCounterStore } from '@/stores/counter'
// 2. 执行方法得到store实例对象
const counterStore = useCounterStore()
console.log(counterStore)
</script>

<template>
  <button @click="counterStore.increment"> {{ counterStore.count}} </button>
</template>

<style scoped>

</style>

getters和异步action

在counter.js文件中进行如下定义

import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import axios from 'axios'

const API_URL = "http://geek.itheima.net/v1_0/channels"
export const useCounterStore = defineStore('counter', () => {
    // 定义数据(state)
    const count = ref(0)

    // 定义修改数据的方法(action 同步+异步)
    const increment = () => {
        count.value++
    }

    // getter定义
    const doubleCount = computed(() => count.value * 2)

    // 定义异步action
    const list = ref([])
    const getList = async () => {
        const res = await axios.get(API_URL)
        list.value = res.data.data.channels
    }
    // 以对象的方式return供组件使用
    return {
        count,
        doubleCount,
        increment,
        list,
        getList
    }
})

在App.vue中使用

<script setup>
// 1. 导入use打头的方法
import { useCounterStore } from '@/stores/counter'
import { onMounted } from 'vue';
// 2. 执行方法得到store实例对象
const counterStore = useCounterStore()
console.log(counterStore)
onMounted(()=>{ // 挂载期发起请求
  counterStore.getList()
})
</script>

<template>
  <button @click="counterStore.increment"> {{ counterStore.count}} </button>
    {{ counterStore.doubleCount }}
  


  <ul>
    <li v-for="item in counterStore.list" :key="item.id">{{ item.name }}</li>
  </ul>
</template>

<style scoped>

</style>

使用storeToRefs进行结构赋值,保持响应式更新

<script setup>
// 1. 导入use打头的方法
import { useCounterStore } from '@/stores/counter'
import { onMounted } from 'vue';
import { storeToRefs } from 'pinia'
// 2. 执行方法得到store实例对象
const counterStore = useCounterStore()
console.log(counterStore)

// 直接结构赋值(响应式丢失)
// const { count, doubleCount} = counterStore
// console.log(count, doubleCount)

// 方法包裹(保持响应式更新)
const { count, doubleCount } = storeToRefs(counterStore)
console.log(count, doubleCount)

// 方法直接从原来的counterStore解构赋值
const { increment } = counterStore

// 触发action
onMounted(()=>{ 
  counterStore.getList()
})
</script>

<template>
  <button @click="increment"> {{ count }} </button>
    {{ doubleCount }}
  


  <ul>
    <li v-for="item in counterStore.list" :key="item.id">{{ item.name }}</li>
  </ul>
</template>

<style scoped>

</style>

Vue Pinia 是一个状态管理库,可以用来管理 Vue.js 应用程序中的状态。下面是使用 Vue Pinia 的一些基本步骤: 1. 安装 Vue Pinia 你可以通过 npm 或 yarn 来安装 Vue Pinia: ``` npm install pinia ``` 或者 ``` yarn add pinia ``` 2. 创建 Pinia 实例 在 Vue 应用程序的入口文件中,创建一个 Pinia 实例,并将其挂载到 Vue 实例中: ```javascript import { createApp } from 'vue' import { createPinia } from 'pinia' import App from './App.vue' const pinia = createPinia() const app = createApp(App) app.use(pinia) app.mount('#app') ``` 3. 定义状态 使用 `defineStore` 方法来定义状态: ```javascript import { defineStore } from 'pinia' export const useCounterStore = defineStore({ id: 'counter', state: () => ({ count: 0 }), actions: { increment() { this.count++ } } }) ``` 在上面的示例中,我们定义了一个名为 `useCounterStore` 的状态库,其中包含一个名为 `count` 的状态和一个名为 `increment` 的操作。 4. 在组件中使用状态 使用 `useStore` 方法来在组件中使用状态: ```javascript import { defineComponent } from 'vue' import { useCounterStore } from './store' export default defineComponent({ setup() { const counterStore = useCounterStore() return { count: counterStore.count, increment: counterStore.increment } } }) ``` 在上面的示例中,我们在组件中使用了名为 `useCounterStore` 的状态库,并从中获取了 `count` 和 `increment` 状态。 这些是使用 Vue Pinia 的基本步骤。当然,Vue Pinia 还有许多其他功能,例如插件和插件选项,可以帮助你更好地管理 Vue.js 应用程序中的状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值