欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript Pinia代替 Vuex的可行性分析

 更新時(shí)間:2022年07月29日 11:55:03   作者:前端小小白zyw  
這篇文章主要介紹了JavaScript中Pinia是否可以代替Vuex,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一、介紹

Vue 3.0 出來(lái)之前,數(shù)據(jù)存儲(chǔ)的主流依舊是 Vuex。但是,當(dāng) Vue 3.0 問(wèn)世后,官方為了迎合 Componsition API,進(jìn)而設(shè)計(jì)了全新的狀態(tài)管理 Pinia,而且未來(lái)的 Vuex 5 中提及的功能點(diǎn)正是 Pinia 目前已實(shí)現(xiàn)的。

! Vuex 5 提及的功能點(diǎn):

  • 更好的兼容 Componsition AP
  • 更好的支持 TypeScript
  • 優(yōu)化 modules 的嵌套結(jié)構(gòu)
  • 支持熱加載,無(wú)需刷新頁(yè)面

二、對(duì)比

PiniaVuex 對(duì)比:

  1. Pinia 沒(méi)有 mutations,且 actions 支持同步/異步,Vuex 同步 mutations,異步 actions
  2. Pinia 相比 Vuex 擁有更好的 TypeScript 支持
  3. Pinia 不再需要 modules 的嵌套結(jié)構(gòu),獨(dú)立的 store,扁平化處理得以更好的代碼分割
  4. Pinia 支持熱加載,無(wú)需重載頁(yè)面,保持現(xiàn)有狀態(tài)
  5. PiniaVue 3 中支持緩存,而 Vuex 則不再支持
  6. Pinia 相比 Vuex 更加輕量化(體積約為 1K)
  7. Pinia 支持 $reset 方法,可以初始化 state
  8. Pinia 不支持 time-travel 功能,相對(duì)于 Vuex 調(diào)試功能還不成熟
  9. Pinia 同樣也支持 Vue 2.0

三、使用

1. 安裝

npm i pinia@next -S

2. 注冊(cè)

index.js

import { createApp } from 'vue'
import element from 'element-plus'
import '@/assets/css/index.css'
import router from '@/router/index'
import App from './App'
import { createPinia } from 'pinia'
const app = createApp(App)
app.use(element).use(router).use(createPinia()).mount('#root')

3. 創(chuàng)建單個(gè) store

store/user.js

import { defineStore } from 'pinia'
export const userStore = defineStore('user', {
  state: _ => {
    return {
      username: '張同學(xué)'
    }
  },
  getters: {
  },
  actions: {
    // 重置數(shù)據(jù)
    _reset() {
      this.$reset()
    }
  }
})

4. 組件內(nèi)引用

Home.vue

<template>
  <div>
    姓名:{{ user_store.username }}
    <br />
    <el-button @click="change_username">修改名稱(chēng)</el-button>
    <el-button @click="user_store._reset()">重置</el-button>
  </div>
</template>
<script>
import { userStore } from '../store/user'
export default {
  setup() {
    let user_store = userStore() // 初始化 user_store
    
    return { user_store }
  }
}
</script>

5. 更新 store  數(shù)據(jù)的4種方式

(1)直接修改特定的值

let user_store = userStore()
/**
 * @description 修改用戶(hù)名稱(chēng)
 */
let update = _ => {
  user_store.username = '張三'
}

(2)利用 $patch 批量修改

let user_store = userStore()
/**
 * @description 修改用戶(hù)名稱(chēng)
 */
let update = _ => {
  user_store.$patch({
  	username: '張三' 
  })
}

(3)利用 $patch 單個(gè)修改特定的值

let user_store = userStore()
/**
 * @description 修改用戶(hù)名稱(chēng)
 */
let update = _ => {
  user_store.$patch(state => {
    state.username = '張三'
  })
}

(4)利用 Actions 修改

let user_store = userStore()
/**
 * @description 修改用戶(hù)名稱(chēng)
 */
let update = _ => {
  user_store.update_username('張三')
}
// 修改用戶(hù)名稱(chēng)
update_username(value) {
  this.username = value
}

6. 其他

從下面的截圖我看可以看到自身的一些屬性、方法等,諸如:$reset$patch 以及熱更新相關(guān)的方法

在這里插入圖片描述

關(guān)于 Pinia 數(shù)據(jù)持久化的問(wèn)題,暫時(shí)沒(méi)有完美的解決方案,待研究

四、總結(jié)

由于 Pinea 是輕量級(jí)的,體積很小,它適合于中小型應(yīng)用。它也適用于低復(fù)雜度的 Vue.js 項(xiàng)目,因?yàn)橐恍┱{(diào)試功能,如時(shí)間旅行和編輯仍然不被支持。未來(lái)將 Vuex 用于中小型 Vue.js 項(xiàng)目是過(guò)度的,因?yàn)樗亓考?jí)的,對(duì)性能有很大影響。因此,Vuex 適用于重量級(jí)、高復(fù)雜度的 Vue.js 項(xiàng)目。

到此這篇關(guān)于JavaScript Pinia代替 Vuex的可行性分析的文章就介紹到這了,更多相關(guān)JavaScript Pinia代替 Vuex內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論