Vue3学习——pinia

官方:Pinia 是 Vue 的存储库,它允许您跨组件/页面共享状态。

安装

npm i pinia

引入

import { createPinia } from 'pinia'
const pinia = createPinia()
app.use(pinia)

此时浏览器的vuetool中就会有个菠萝🍍的图标

使用

store/count.ts

import { defineStore } from 'pinia'
export const useCountStore = defineStore('count', {
	state(){
		return {
			token: 'xin',
			name: 'kele'
		}
	},
	action:{},
	getter:{}
})

vue页面

import { useCountStore  } from '@/store/count.ts'
const countStore = useCountStore()
// 获取state中的token
console.log(countStore.token)

修改数据

在vuex中不可以直接改state的值,但pinia可以

  • 方式一
// 直接vue文件改
countStore.token = 'xxin'
  • 方式二
// 批量修改
countStore.$patch({
	token: 'xx',
	name: 'kelele'
})

观察工具pinia是批量变更是一次,所以批量变更推荐使用$patch

  • 方式三
// actions写方法里
export const useCountStore = defineStore('count', {
	state(){
		return {
			token: 'xin',
			name: 'kele'
		}
	},
	action:{
		changeToken() {
			this.token = 'xinxin'
		}
	},
	getter:{}
})

storeToRefs

用于解构store里的数据(不会对方法进行包裹,只对数据ref)

import { storeToRefs } from 'pinia'
const { name } = storeToRefs(countStore)

getter使用

import { defineStore } from 'pinia'
export const useCountStore = defineStore('count', {
	state(){
		return {
			token: 'xin',
			name: 'kele'
		}
	},
	action:{},
	getter:{
		// 箭头函数不能用this
		addToken: state => state.token + '~~',
		// 不传参数state会飘红,不知道return的是什么类型;加个string类型就可以了
		upperName():string{
			return {
				this.name.toUpperCase()
			}
		}
	}
})

getter的数据也可解构拿到

$subscribe

相当于watch

// 监听数据的变化
store.$subscribe((args, state) => {
  console.log('args', args)
  console.log('state', state)
})
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3 是一个流行的 JavaScript 框架,而 Pinia 是一个基于 Vue 3 的状态管理库。要在 Vue 3 中使用 Pinia,你需要进行以下步骤: 1. 安装 Pinia:你可以使用 npm 或者 yarn 安装 Pinia。 使用 npm: ``` npm install pinia ``` 使用 yarn: ``` yarn add pinia ``` 2. 创建 Pinia 实例:在你的应用程序的入口文件中,创建一个 Pinia 实例。 ```javascript import { createPinia } from 'pinia'; const pinia = createPinia(); ``` 3. 配置 Vue 3:在应用程序的入口文件中,配置 Vue 3 以使用 Pinia。 ```javascript import { createApp } from 'vue'; import App from './App.vue'; import { pinia } from './pinia'; const app = createApp(App); app.use(pinia); app.mount('#app'); ``` 4. 创建 store:在你的项目中创建一个 store,它将承载你的状态和操作。 ```javascript import { defineStore } from 'pinia'; export const useStore = defineStore('store', { state: () => ({ count: 0, }), actions: { increment() { this.count++; }, }, }); ``` 5. 在组件中使用 store:在你的组件中使用创建的 store。 ```vue <template> <div> <p>Count: {{ count }}</p> <button @click="increment">Increment</button> </div> </template> <script> import { useStore } from './store'; export default { setup() { const store = useStore(); return { count: store.count, increment: store.increment, }; }, }; </script> ``` 这就是如何在 Vue 3 中使用 Pinia。通过使用 Pinia,你可以更方便地管理应用程序的状态。记得引入相关的依赖,然后按照上述步骤进行操作即可。希望对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值