關(guān)于pinia的簡單使用方式
pinia
1.pinia的安裝
npm install pinia
2.使用pinia創(chuàng)建一個(gè)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
目錄下創(chuàng)建store
文件夾,在store
文件夾下創(chuàng)建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
里面定義一個(gè)helloworld
03.在默認(rèn)模板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
給結(jié)構(gòu)出來,pinia
中給我們提供了一個(gè)方法storeToRefs()
,這樣我們頁面上就顯示了兩個(gè)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>
3.pinia修改數(shù)據(jù)的4方式
01.在store
中的index.js
中新增一個(gè)count:0;
然后在HelloWorld.vue
中添加一個(gè)button
,點(diǎn)擊按鈕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">修改數(shù)據(jù)</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
對象傳遞的方式(多數(shù)據(jù)情況下,相對01來說,更推薦使用這個(gè)方法,官方說$patch
優(yōu)化了),index.js
代碼不變
- HelloWorld.vue
<template> <div>{{ store.helloWorld }}</div> <div>{{ helloWorld }}</div> <div>{{ count }}</div> <el-button type='primary' @click="changeCount">修改數(shù)據(jù)</el-button> <el-button type='primary' @click="changeCountPatch">修改數(shù)據(jù)($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
函數(shù)傳遞的方式(更適合處理復(fù)雜的業(yè)務(wù)邏輯),index.js
代碼不變
- HelloWorld.vue
<template> <div>{{ store.helloWorld }}</div> <div>{{ helloWorld }}</div> <div>{{ count }}</div> <el-button type='primary' @click="changeCount">修改數(shù)據(jù)</el-button> <el-button type='primary' @click="changeCountPatch">修改數(shù)據(jù)($patch方法對象)</el-button> <el-button type='primary' @click="changeCountPatch2">修改數(shù)據(jù)($patch方法函數(shù))</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.業(yè)務(wù)邏輯更復(fù)雜的情況下,在index.js的actions中定義函數(shù)調(diào)用
- 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">修改數(shù)據(jù)</el-button> <el-button type='primary' @click="changeCountPatch">修改數(shù)據(jù)($patch方法對象)</el-button> <el-button type='primary' @click="changeCountPatch2">修改數(shù)據(jù)($patch方法函數(shù))</el-button> <el-button type='primary' @click="changeStateActions">修改數(shù)據(jù)(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>
4.getters的使用
? 類似于vue中的計(jì)算屬性,比如我們有這樣一個(gè)需求,就是在state
里有有一個(gè)狀態(tài)數(shù)據(jù)是電話號碼,我們想輸出的時(shí)候把中間四位展示為****
.這時(shí)候用getters
就是非常不錯(cuò)的選擇。
- 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">修改數(shù)據(jù)</el-button> <el-button type='primary' @click="changeCountPatch">修改數(shù)據(jù)($patch方法對象)</el-button> <el-button type='primary' @click="changeCountPatch2">修改數(shù)據(jù)($patch方法函數(shù))</el-button> <el-button type='primary' @click="changeStateActions">修改數(shù)據(jù)(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>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)配置全局訪問路徑頭(axios)
今天小編就為大家分享一篇vue實(shí)現(xiàn)配置全局訪問路徑頭(axios),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11前端插件庫之vue3使用vue-codemirror插件的步驟和實(shí)例
CodeMirror是一款基于JavaScript、面向語言的前端代碼編輯器,下面這篇文章主要給大家介紹了關(guān)于前端插件庫之vue3使用vue-codemirror插件的步驟和實(shí)例,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07vue 對象添加或刪除成員時(shí)無法實(shí)時(shí)更新的解決方法
這篇文章主要介紹了vue 對象添加或刪除成員時(shí)無法實(shí)時(shí)更新的解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05vite+vue3+tsx項(xiàng)目打包后動態(tài)路由無法加載頁面的問題及解決
這篇文章主要介紹了vite+vue3+tsx項(xiàng)目打包后動態(tài)路由無法加載頁面的問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03解決antd datepicker 獲取時(shí)間默認(rèn)少8個(gè)小時(shí)的問題
這篇文章主要介紹了解決antd datepicker 獲取時(shí)間默認(rèn)少8個(gè)小時(shí)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10vue-cli3 項(xiàng)目從搭建優(yōu)化到docker部署的方法
這篇文章主要介紹了vue-cli3 項(xiàng)目從搭建優(yōu)化到docker部署的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01淺析Proxy可以優(yōu)化vue的數(shù)據(jù)監(jiān)聽機(jī)制問題及實(shí)現(xiàn)思路
這篇文章主要介紹了淺析Proxy可以優(yōu)化vue的數(shù)據(jù)監(jiān)聽機(jī)制問題及實(shí)現(xiàn)思路,需要的朋友可以參考下2018-11-11Vue利用localStorage本地緩存使頁面刷新驗(yàn)證碼不清零功能的實(shí)現(xiàn)
這篇文章主要介紹了Vue利用localStorage本地緩存使頁面刷新驗(yàn)證碼不清零功能的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Vue實(shí)現(xiàn)base64編碼圖片間的切換功能
這篇文章主要介紹了Vue實(shí)現(xiàn)base64編碼圖片間的切換功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12