最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue生態(tài)的新成員Pinia的詳細(xì)介紹

 更新時(shí)間:2022年01月13日 11:04:00   作者:太涼  
本文主要介紹了Vue生態(tài)的新成員Pinia的詳細(xì)介紹,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Pinia是Vue應(yīng)用程序的狀態(tài)管理方案,是Vuex核心團(tuán)隊(duì)成員開(kāi)發(fā)。感覺(jué)更像是常規(guī)的舊 javascript 導(dǎo)入模塊,實(shí)現(xiàn)了很多Vuex5的提案。

Pinia同時(shí)支持Vue2和Vue3,不過(guò)下面展示的例子都是使用Vue3,Pinia的版本是Pinia@2.0.9。

Vue2和Vue3使用的Pinia版本有一點(diǎn)不同,因此請(qǐng)查看官方 Pinia 文檔以獲取更多信息。

安裝和配置

可以使用npm或者yarn安裝Pinia

yarn add pinia@next
# 或者 使用npm
npm install pinia@next

安裝完畢后,找到Vue應(yīng)用程序的文件 main.js (vue-cli初始化的項(xiàng)目,一般都命名為main.js)。從Pinia的npm包中導(dǎo)入createPinia,然后使用Vue的use方法作為插件使用

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

createApp(App)
? .use(router)
? .use(createPinia())
? .mount('#app')

接下來(lái),我們?nèi)?chuàng)建一個(gè) store

Store核心

Pinia的Store不同于Vuex的Store。使用Vuex的時(shí)候,整個(gè)應(yīng)用程序只有一個(gè)主要Store(雖然你可以拆成不同的模塊,但是還是一個(gè)主存儲(chǔ)出口)。然而,今天的主角Pinia則是開(kāi)箱即用的模塊化設(shè)計(jì),不在需要一個(gè)主要的Store,可以創(chuàng)建不同的Store。例如,我們可以創(chuàng)建一個(gè)登錄用戶Store。

// store/loggedInUser.js
import { defineStore } from 'pinia'

export const useLoggedInUserStore = defineStore({
// id 是必填的,并且所有 Store 中唯一。因?yàn)镻inia會(huì)將它在devtools顯示
? id: 'loggedInUser',
? state () {
? ? return {
? ? ? name: '太涼',
? ? ? age: 18,
? ? ? email: 'fake@email.com'
? ? }
? },
? getters: {},
? actions: {}
})

上面我們創(chuàng)建了一個(gè)登錄用戶Store,接下來(lái)我們可以直接在組件中引用,然后在setup函數(shù)中調(diào)用useLoggedInUserStore

<template>
? <div>PiniaApage</div>
? <div>姓名:{{user.name}}</div>
? <div>年齡:{{user.age}}</div>
</template>

<script>
import { useLoggedInUserStore } from '@/store/loggedInUser.js'
export default {
? name: 'PiniaDemoPage1',
? setup () {
? ? const user = useLoggedInUserStore()
? ? return {
? ? ? user
? ? }
? }
}
</script>

這與 Vuex 有很大不同,Vuex 的Store  會(huì)自動(dòng)掛載到 當(dāng)前Vue的實(shí)例,可以通過(guò)this.$store的方式調(diào)用。然而,Pania 方式也讓開(kāi)發(fā)人員 更清楚Store來(lái)自哪里,因?yàn)樗菢?biāo)準(zhǔn)的 Javascript 模塊導(dǎo)入,可以看到是從哪個(gè)文件導(dǎo)入的。

假如,你不用  Composition API的方式,你仍然可以借助一些輔助函數(shù)使用Pinia,下面將會(huì)詳細(xì)說(shuō),這里先提一嘴。

State

我們將 Store 中的 state 屬性設(shè)置為一個(gè)函數(shù),該函數(shù)返回一個(gè)包含不同狀態(tài)值的對(duì)象。這與我們?cè)诮M件中定義data的方式非常相似。事實(shí)上,唯一的區(qū)別是屬性名稱(chēng):狀態(tài)與數(shù)據(jù)

import { defineStore } from 'pinia'
export const useLoggedInUserStore = defineStore({
// id 是必填的,并且所有 Store 中唯一。因?yàn)镻inia會(huì)將它在devtools顯示
? id: 'loggedInUser',
? state () {
? ? return {
? ? ? name: '太涼',
? ? ? age: 18,
? ? ? email: 'fake@email.com'
? ? }
? },
? getters: {},
? actions: {}
})

現(xiàn)在,為了從組件中訪問(wèn) loginUserStore 的狀態(tài),我們只需要引用我們需要的Store,這種方式非常優(yōu)雅。完全不需要像Vuex那樣從嵌套對(duì)象中找到我們需要的Store。

<template>
? <div>PiniaApage</div>
? <div>姓名:{{user.name}}</div>
? <div>年齡:{{user.age}}</div>
</template>

<script>
import { useLoggedInUserStore } from '@/store/loggedInUser.js'
export default {
? name: 'PiniaDemoPage1',
? setup () {
? ? //不用在想以前那個(gè) user.state.name的方式獲取了
? ? const user = useLoggedInUserStore()
? ? return {
? ? ? user
? ? }
? }
}
</script>

警告,不能結(jié)構(gòu)user,因?yàn)槟菢訒?huì)失去響應(yīng)式。下面的方式是錯(cuò)誤的。

? const {name, email} = useLoggedInUserStore()

如果你使用的不是Composition API的方式,而是Option API的方式??梢酝ㄟ^(guò)Pinia的mapState函數(shù)獲取 State數(shù)據(jù)。Pinia的mapState函數(shù) 和Vuex的mapState雖然名字相同,但是使用方式完全不同。

Pinia的mapState函數(shù)的第一個(gè)參數(shù)是必須是之前創(chuàng)建的Store,第二個(gè)參數(shù)是Store中的state的屬性值。看代碼

//PageComponent.vue
<template>
? <h1>你好, 我是 {{name}},我來(lái)自地球</h1>
? <h2>聯(lián)系郵箱:{{email}}</h2>
</template>
<script>
import {mapState} from 'pinia'
export default{
? computed:{
? ? ...mapState(useLoggedInUserStore, ['name','email'])
? }
}
</script>

總結(jié):

  • 定義Pinia的state,和組件的data的方式是一樣
  • 需要在組件間中手動(dòng)導(dǎo)入我們需要的Store模塊,這樣做的好處是明確知道數(shù)據(jù)的來(lái)源,更符合標(biāo)準(zhǔn)的 Javascript

Getters

Pinia中的getters和Vuex中的getters 的作用是相同的,都是作為組件的計(jì)算屬性(computed)。
創(chuàng)建getters的方式有兩種,一種是通過(guò)this關(guān)鍵字,一種是通過(guò)state具體看代碼

//store/usePostsStore.js
import { defineStore } from 'pinia'

export const usePostsStore = defineStore({
? id: 'PostsStore',
? state: ()=>({ posts: ['post 1', 'post 2', 'post 3', 'post 4'] }),
? getters:{

? ? // 傳統(tǒng)函數(shù)方式
? ? postsCount: function(){
? ? ? return this.posts.length
? ? },
? ? // 傳統(tǒng)函數(shù)簡(jiǎn)寫(xiě)方式
? ? postsCount2(){
? ? ? return this.posts.length
? ? },
? ? // 箭頭函數(shù)
? ? postsCount3:(state)=>state.posts.length,

? ? // ? 不能用箭頭函數(shù)+this的方式,這種方式this指向不對(duì)
? ? // postsCount: ()=> this.posts.length
? }
})

接下來(lái)看Composition API方式的組件中如何使用創(chuàng)建的getters,其實(shí)用法和state相同??创a

<template>
? <div>PiniaBpage</div>
? <div> 總數(shù):{{postsStore.postsCount}}</div>
</template>

<script>
import { usePostsStore } from '@/store/usePostsStore.js'
export default {
? name: 'PiniaBpage',
? setup () {
? ? const postsStore = usePostsStore()
? ? return {
? ? ? postsStore
? ? }
? }
}
</script>

如果是Option API 的組件,不能像Vuex那樣通過(guò)mapGetters輔助函數(shù)獲取。因?yàn)镻inia沒(méi)有mapGetters輔助函數(shù),Pinia中消費(fèi)getters還是借助 mapState輔助函數(shù)

<template>
? <div> 總數(shù):{{postsCount}}</div>
</template>

<script>
import { mapState } from 'pinia'
import { usePostsStore } from "@/store/PostsStore";

export default {
? computed:{
? ? ...mapState(usePostsStore, ['postsCount'])
? }
};
</script>

Actions

Pinia不同于Vuex,Pinia提供了單一的方式更改state的值,在Pinia中沒(méi)有mutations,只有action方式。先來(lái)看一下Pinia的action怎么用吧。上代碼

  • 直接通過(guò)this找到對(duì)應(yīng)的state修改
  • 通過(guò).$patch 函數(shù)方法
  • 通過(guò).$patch 對(duì)象方法
import { defineStore } from 'pinia'

export const usePostsStore = defineStore({
? id: 'PostsStore',
? state: () => ({
? ? posts: ['post 1', 'post 2', 'post 3', 'post 4'],
? ? user: { postsCount: 2 },
? ? age:18,
? ? errors: []
? }),
? getters: {
? ? postsCount: function () {
? ? ? return this.posts.length
? ? },
? ? postsCount2 () {
? ? ? return this.posts.length
? ? },
? ? // 箭頭函數(shù)
? ? postsCount3: (state) => state.posts.length
? },
? actions: {
? ? insertPost () {
? ? ? //方式1:直接通過(guò)this找到對(duì)應(yīng)的state修改
? ? ? this.posts.push(`post_${Date.now()}`)
? ? ? this.user.postsCount++
? ? },
? ? removePost () {
? ? ? //方式2:通過(guò).$patch 函數(shù)方法
? ? ? this.$patch((state) => {
? ? ? ? state.posts.shift()
? ? ? ? state.user.postsCount++
? ? ? })
? ? ??
? ? ? //通過(guò).$patch 對(duì)象方法
? ? ? this.$patch({
? ? ? ? age:30
? ? ? });
? ? }
? }
})

以上展示了三種更改Pinia的State方式。

如果是 Composition API使用方式

<template>
? <div>PiniaBpage</div>
? <div> 總數(shù):{{postsStore.postsCount}}</div>
? <ul>
? ? <li
? ? ? v-for="item in postsStore.posts"
? ? ? :key="item"
? ? >
? ? ? {{item}}
? ? </li>
? </ul>
? <button @click="handleAdd">新增</button>
? <button @click="handleRemove">刪除</button>
? <button @click="handleBeforeAdd">新增到前面</button>
</template>

<script>
import { usePostsStore } from '@/store/usePostsStore.js'
??
export default {
? name: 'PiniaBpage',
? setup () {
? ? const postsStore = usePostsStore()

? ? // 新增
? ? const handleAdd = () => {
? ? ? postsStore.insertPost()
? ? }

? ? // 刪除
? ? const handleRemove = () => {
? ? ? postsStore.removePost()
? ? }
? ? // 新增到前面,也可以在這里通過(guò)$patch修改,同樣這里也可以直接修改
? ?const ?handleBeforeAdd=()=>{
? ? ?postsStore.$patch((state) => {
? ? ? ? state.posts.shift()
? ? ? ? state.user.postsCount++
? ? ?})
? ?}
? ? return {
? ? ? postsStore,
? ? ? handleAdd,
? ? ? handleRemove,
? ? ? handleBeforeAdd
? ? }
? }
}
</script>

如果是 Options API使用方式,需要借助 輔助函數(shù) mapActions

// PostEditorComponent.vue
<template>
? <input type="text" v-model="post" />
? <button @click="insertPost(post)">保存</button>
</template>
<script>
import {mapActions} from 'pinia'
import { usePostsStore } from '@/store/PostsStore';
export default{
? data(){
? ? return { post: '' }
? },?
? methods:{
? ? ...mapActions(usePostsStore, ['insertPost'])
? }
}
</script>

其實(shí)Pinia的action使用非常靈活

  • 可以在組件或者其他actions里面調(diào)用
  • 可以在其他的Store的actions里面調(diào)用
import { useAuthStore } from './auth-store'

export const useSettingsStore = defineStore('settings', {
? state: () => ({
? ? // ...
? }),
? actions: {
? ? async fetchUserPreferences(preferences) {
? ? ? const auth = useAuthStore()
? ? ? if (auth.isAuthenticated) {
? ? ? ? this.preferences = await fetchPreferences()
? ? ? } else {
? ? ? ? throw new Error('User must be authenticated')
? ? ? }
? ? },
? },
})
  • 支持同步和異步
  • 可以支持靈活的參數(shù)
  • ...............

Vue Devtools

在 Vue 2 中,Pania 支持在 Vuex 選項(xiàng)卡中查看狀態(tài),甚至可以看到時(shí)間軌跡。時(shí)間軌跡的標(biāo)簽幾乎沒(méi)有在 Vuex 中使用時(shí)那么好。
至于 Vue 3,Pania 僅支持在 devtools 中查看狀態(tài),不支持時(shí)間軌跡功能。然而,這實(shí)際上比 Vuex 為 Vue 3 提供的要多,因?yàn)樗谧钚碌拈_(kāi)發(fā)工具中根本不支持。

最后

快速回顧一下 Pinia 最顯著的功能,以幫助你去快速了解Pinia,并應(yīng)用于項(xiàng)目中

  • 由 Vue.js 核心團(tuán)隊(duì)成員維護(hù)
  • 感覺(jué)更像是常規(guī)的舊 javascript 導(dǎo)入模塊,將操作為方法調(diào)用,直接在store上訪問(wèn)狀態(tài)等。
  • 不再mutations
  • 與 Vue Devtools 集成

結(jié)論

Pinia雖然是Vue生態(tài)的新成員,但是事實(shí)證明Pinia是最優(yōu)前途的狀態(tài)管理解決方案,具有直觀的API,模塊化,清晰導(dǎo)入來(lái)源。

參考文獻(xiàn)

Pinia, an Alternative Vue.js Store
官網(wǎng)

到此這篇關(guān)于Vue生態(tài)的新成員Pinia的詳細(xì)介紹的文章就介紹到這了,更多相關(guān)Vue Pinia內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 適用于 Vue 的播放器組件Vue-Video-Player操作

    適用于 Vue 的播放器組件Vue-Video-Player操作

    這篇文章主要介紹了適用于 Vue 的播放器組件Vue-Video-Player操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11
  • Vue實(shí)現(xiàn)頁(yè)面刷新跳轉(zhuǎn)到當(dāng)前頁(yè)面功能

    Vue實(shí)現(xiàn)頁(yè)面刷新跳轉(zhuǎn)到當(dāng)前頁(yè)面功能

    在Vue.js應(yīng)用開(kāi)發(fā)中,有時(shí)候我們需要實(shí)現(xiàn)頁(yè)面的刷新或跳轉(zhuǎn)到當(dāng)前頁(yè)面的功能,這種需求在某些特定場(chǎng)景下非常有用,本文將詳細(xì)介紹如何在Vue中實(shí)現(xiàn)頁(yè)面刷新和跳轉(zhuǎn)到當(dāng)前頁(yè)面的功能,并提供多個(gè)示例和使用技巧,需要的朋友可以參考下
    2024-10-10
  • 基于vue-cli 打包時(shí)抽離項(xiàng)目相關(guān)配置文件詳解

    基于vue-cli 打包時(shí)抽離項(xiàng)目相關(guān)配置文件詳解

    下面小編就為大家分享一篇基于vue-cli 打包時(shí)抽離項(xiàng)目相關(guān)配置文件詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • vue下跨域設(shè)置的相關(guān)介紹

    vue下跨域設(shè)置的相關(guān)介紹

    本篇文章主要介紹了vue下跨域設(shè)置的相關(guān)介紹,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • Vue中transition標(biāo)簽的基本使用教程

    Vue中transition標(biāo)簽的基本使用教程

    Vue提供了transition的封裝組件,可以給任何元素和組件添加進(jìn)入/離開(kāi)過(guò)渡,下面這篇文章主要給大家介紹了關(guān)于Vue中transition標(biāo)簽基本使用的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • vue組件 $children,$refs,$parent的使用詳解

    vue組件 $children,$refs,$parent的使用詳解

    本篇文章主要介紹了vue組件 $children,$refs,$parent的使用詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • vue中使用sessionStorage記住密碼功能

    vue中使用sessionStorage記住密碼功能

    sessionStorage不是一種持久化的本地存儲(chǔ),僅僅是會(huì)話級(jí)別的存儲(chǔ)。這篇文章主要介紹了vue中使用sessionStorage記住密碼功能,需要的朋友可以參考下
    2018-07-07
  • vue根據(jù)條件不同顯示不同按鈕的操作

    vue根據(jù)條件不同顯示不同按鈕的操作

    這篇文章主要介紹了vue根據(jù)條件不同顯示不同按鈕的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-08-08
  • Vue form表單動(dòng)態(tài)添加組件實(shí)戰(zhàn)案例

    Vue form表單動(dòng)態(tài)添加組件實(shí)戰(zhàn)案例

    這篇文章主要介紹了Vue form表單動(dòng)態(tài)添加組件實(shí)戰(zhàn)案例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Vue如何通過(guò)Vue.prototype定義原型屬性實(shí)現(xiàn)全局變量

    Vue如何通過(guò)Vue.prototype定義原型屬性實(shí)現(xiàn)全局變量

    在Vue.js開(kāi)發(fā)中,通過(guò)原型屬性為Vue實(shí)例添加全局變量是一種常見(jiàn)做法,使用$前綴命名,可以避免與組件內(nèi)部的數(shù)據(jù)、方法或計(jì)算屬性產(chǎn)生命名沖突,這種方式簡(jiǎn)單有效,確保了變量在所有Vue實(shí)例中的可用性,同時(shí)保持全局作用域的整潔
    2024-10-10

最新評(píng)論

横峰县| 基隆市| 买车| 鄂伦春自治旗| 来宾市| 通山县| 普兰店市| 大悟县| 广元市| 万荣县| 上蔡县| 盐边县| 永昌县| 泸西县| 永宁县| 韶关市| 三亚市| 北票市| 庆安县| 当阳市| 婺源县| 南汇区| 五指山市| 兴山县| 黎川县| 和龙市| 修文县| 綦江县| 鲁山县| 宜兴市| 波密县| 江北区| 沛县| 安新县| 儋州市| 太仓市| 栾城县| 丹寨县| 茌平县| 南昌县| 延长县|