pinia简单使用

pinia

1.pinia的安装

npm install pinia

2.使用pinia创建一个store

​ 01.在main.js引入

​ index.js

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

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

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

​ 02.在src目录下创建store文件夹,在store文件夹下创建index.js文件

​ index.js

import { defineStore } from 'pinia'
import { demoStore } from './demo'

export const mainStore = defineStore('main', {
    state: () => {
        return {
            helloWorld: 'Hello World!'
        }
    },
    getters: {},
    actions: {}
})

​ 在state里面定义一个helloworld

​ 03.在默认模板Helloworld.vue中使用,直接使用{{store.helloWorld}}即可显示在界面上

​ HelloWorld.vue

<template>
    {{ store.helloWorld }}
</template>

<script setup>
import { mainStore } from '../store/index'

const store = mainStore()

</script>

<style lang='scss' scoped></style>

​ 我们可以把helloworld给结构出来,pinia中给我们提供了一个方法storeToRefs(),这样我们页面上就显示了两个Hello World!

​ HelloWorld.vue

<template>
    <div>{{ store.helloWorld }}</div>
    <div>{{ helloWorld }}</div>
</template>

<script setup>
import { mainStore } from '../store/index'
import { storeToRefs } from 'pinia'

const store = mainStore()
const { helloWorld } = storeToRefs(store)

</script>
<style lang='scss' scoped></style>

2.pinia修改数据的4方式

​ 01.在store中的index.js中新增一个count:0;然后在HelloWorld.vue中添加一个button,点击按钮count加1

​ HelloWorld.vue

import { defineStore } from 'pinia'

export const mainStore = defineStore('main', {
    state: () => {
        return {
            helloWorld: 'Hello World!',
            count: 0
        }
    },
    getters: {},
    actions: {}
})

​ HelloWorld.vue

<template>
    <div>{{ store.helloWorld }}</div>
    <div>{{ helloWorld }}</div>
    <div>{{ count }}</div>
    <el-button type='primary' @click="changeCount">修改数据</el-button>
</template>

<script setup>
import { mainStore } from '../store/index'
import { storeToRefs } from 'pinia'

const store = mainStore()
const { helloWorld, count } = storeToRefs(store)

const changeCount = () => {
    store.count++
}

</script>

<style lang='scss' scoped></style>

​ 02.使用$patch对象传递的方式(多数据情况下,相对01来说,更推荐使用这个方法,官方说$patch优化了),index.js代码不变

​ HelloWorld.vue

<template>
    <div>{{ store.helloWorld }}</div>
    <div>{{ helloWorld }}</div>
    <div>{{ count }}</div>
    <el-button type='primary' @click="changeCount">修改数据</el-button>
    <el-button type='primary' @click="changeCountPatch">修改数据($patch方法对象)</el-button>
</template>

<script setup>
import { mainStore } from '../store/index'
import { storeToRefs } from 'pinia'

const store = mainStore()
const { helloWorld, count } = storeToRefs(store)

const changeCount = () => {
    store.count++
}

const changeCountPatch = () => {
    store.$patch({
        count: store.count + 2
    })
}

</script>

<style lang='scss' scoped></style>

​ 03.使用$patch函数传递的方式(更适合处理复杂的业务逻辑),index.js代码不变

​ HelloWorld.vue

	<template>
    <div>{{ store.helloWorld }}</div>
    <div>{{ helloWorld }}</div>
    <div>{{ count }}</div>
    <el-button type='primary' @click="changeCount">修改数据</el-button>
    <el-button type='primary' @click="changeCountPatch">修改数据($patch方法对象)</el-button>
    <el-button type='primary' @click="changeCountPatch2">修改数据($patch方法函数)</el-button>
</template>

<script setup>
import { mainStore } from '../store/index'
import { storeToRefs } from 'pinia'

const store = mainStore()
const { helloWorld, count } = storeToRefs(store)

const changeCount = () => {
    store.count++
}

const changeCountPatch = () => {
    store.$patch({
        count: store.count + 2
    })
}

const changeCountPatch2 = () => {
    store.$patch((store) => {
        store.count = store.count + 3
    })
}

</script>

<style lang='scss' scoped></style>

​ 04.业务逻辑更复杂的情况下,在index.js的actions中定义函数调用

​ index.js

import { defineStore } from 'pinia'

export const mainStore = defineStore('main', {
    state: () => {
        return {
            helloWorld: 'Hello World!',
            count: 0
        }
    },
    getters: {},
    actions: {
        changeState() {
            this.count++
        }
    }
})

​ HelloWorld.vue

<template>
    <div>{{ store.helloWorld }}</div>
    <div>{{ helloWorld }}</div>
    <div>{{ count }}</div>
    <el-button type='primary' @click="changeCount">修改数据</el-button>
    <el-button type='primary' @click="changeCountPatch">修改数据($patch方法对象)</el-button>
    <el-button type='primary' @click="changeCountPatch2">修改数据($patch方法函数)</el-button>
    <el-button type='primary' @click="changeStateActions">修改数据(actions)</el-button>
</template>

<script setup>
import { mainStore } from '../store/index'
import { storeToRefs } from 'pinia'

const store = mainStore()
const { helloWorld, count } = storeToRefs(store)

const changeCount = () => {
    store.count++
}

const changeCountPatch = () => {
    store.$patch({
        count: store.count + 2
    })
}

const changeCountPatch2 = () => {
    store.$patch((store) => {
        store.count = store.count + 3
    })
}

const changeStateActions = () => {
    store.changeState()
}

</script>

<style lang='scss' scoped></style>

3.getters的使用

​ 类似于vue中的计算属性,比如我们有这样一个需求,就是在state里有有一个状态数据是电话号码,我们想输出的时候把中间四位展示为****.这时候用getters就是非常不错的选择。

​ index.js

import { defineStore } from 'pinia'

export const mainStore = defineStore('main', {
    state: () => {
        return {
            helloWorld: 'Hello World!',
            count: 0,
            phone:'15139333888'
        }
    },
    getters: {
        phoneHidden(state){
            return state.phone.toString().replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2')
        }
    },
    actions: {
        changeState() {
            this.count++
        }
    }
})

​ HelloWorld.vue

<template>
    <div>{{ store.helloWorld }}</div>
    <div>{{ helloWorld }}</div>
    <div>{{ count }}</div>
    <div>{{ phoneHidden }}</div>
	
    <el-button type='primary' @click="changeCount">修改数据</el-button>
    <el-button type='primary' @click="changeCountPatch">修改数据($patch方法对象)</el-button>
    <el-button type='primary' @click="changeCountPatch2">修改数据($patch方法函数)</el-button>
    <el-button type='primary' @click="changeStateActions">修改数据(actions)</el-button>
</template>

<script setup>
import { mainStore } from '../store/index'
import { storeToRefs } from 'pinia'

const store = mainStore()
const { helloWorld, count, phoneHidden } = storeToRefs(store)

const changeCount = () => {
    store.count++
}

const changeCountPatch = () => {
    store.$patch({
        count: store.count + 2
    })
}

const changeCountPatch2 = () => {
    store.$patch((store) => {
        store.count = store.count + 3
    })
}

const changeStateActions = () => {
    store.changeState()
}

</script>

<style lang='scss' scoped></style>
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
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 并在组件中使用它们。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

bangbang给你两锤

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

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

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

打赏作者

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

抵扣说明:

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

余额充值