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

Vue3中使用Pinia修改State的五種方式

 更新時間:2023年11月15日 15:03:51   作者:xyphf_和派孔明  
這篇文章主要介紹了Vue3中使用Pinia修改State的五種方式,本文通過實例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧

修改Pinia倉庫的值有5種方式

src/store/index.ts

import { defineStore } from 'pinia';
import { Names } from './store-name';
export const useTestStore = defineStore(Names.Test, {
    state:()=>{
        return {
            current:1111,
            name: '小滿111'
        }
    },
    getters:{ // 類似computed計算屬性 同樣有緩存的
    },
    actions:{ // 類似 methods方法 可以做同步、異步操作 提交state
    }
});

第一種修改State的方式

<template>
    <div>
      pinia: {{ Test.current }} --- {{ Test.name }}
      <button @click="change">change</button>
    </div>
</template>
<script setup lang="ts">
import  {useTestStore} from './store';
const Test = useTestStore();
// 第一種修改State的方式
const change = () => {
   Test.current++
}
</script>
<style scoped>
</style>

第二種修改State的方式

<template>
    <div>
      pinia: {{ Test.current }} --- {{ Test.name }}
      <button @click="change">change</button>
    </div>
</template>
<script setup lang="ts">
import  {useTestStore} from './store';
const Test = useTestStore();
// 第二種修改State的方式 批量修改對象屬性
const change = () => {
  Test.$patch({
    current: 999,
    name: '李四'
  })
}
</script>
<style scoped>
</style>

第三種修改State的方式

<template>
    <div>
      pinia: {{ Test.current }} --- {{ Test.name }}
      <button @click="change">change</button>
    </div>
</template>
<script setup lang="ts">
import  {useTestStore} from './store';
const Test = useTestStore();
// 第三種修改State的方式 向$patch中傳入工廠函數(shù) 可以寫邏輯
const change = () => {
  Test.$patch((state) => { 
    if(state.current>1113){
      state.current--;
      state.name = '羅翔老師';
    } else {
      state.current++;
      state.name = '羅永浩老師';
    }
  })
}
</script>
<style scoped>
</style>

第四種修改State的方式

<template>
    <div>
      pinia: {{ Test.current }} --- {{ Test.name }}
      <button @click="change">change</button>
    </div>
</template>
<script setup lang="ts">
import  {useTestStore} from './store';
const Test = useTestStore();
// 第四種修改State的方式 覆蓋這個state對象
const change = () => {
  Test.$state = {
    current: 1024,
    name: '羅城將軍'
  }
}
</script>
<style scoped>
</style>

第五種修改State的方式

調(diào)用actions里面的方式

在 src/store/index.ts 里面的actions里面寫個方法

import { defineStore } from 'pinia';
import { Names } from './store-name';
export const useTestStore = defineStore(Names.Test, {
    state:()=>{
        return {
            current:1111,
            name: '小滿111'
        }
    },
    getters:{ // 類似computed計算屬性 同樣有緩存的
    },
    actions:{ // 類似 methods方法 可以做同步、異步操作 提交state
        setCurrent(num:number){ // 注意此處不要寫箭頭函數(shù),否則this指向就不對了
            this.current = num;
        }
    }
});

再在組件里面調(diào)用

<template>
    <div>
      pinia: {{ Test.current }} --- {{ Test.name }}
      <button @click="change">change</button>
    </div>
</template>
<script setup lang="ts">
import  {useTestStore} from './store';
const Test = useTestStore();
// 第五種 
const change = () => {
  Test.setCurrent(5173);  // 直接調(diào)用setCurrent函數(shù)
}
</script>
<style scoped>
</style>

到此這篇關(guān)于Vue3中使用Pinia修改State的方法的文章就介紹到這了,更多相關(guān)vue3使用Pinia修改state內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • uniapp開發(fā)打包成H5部署到服務(wù)器的詳細(xì)步驟

    uniapp開發(fā)打包成H5部署到服務(wù)器的詳細(xì)步驟

    前端使用uniapp開發(fā)項目完成后,需要將頁面打包,生成H5的靜態(tài)文件,部署在服務(wù)器上這樣通過服務(wù)器鏈接地址,直接可以在手機(jī)上點開來訪問,下面小編給大家講解uniapp開發(fā)打包成H5部署到服務(wù)器的步驟,感興趣的朋友一起看看吧
    2022-11-11
  • Vue項目如何根據(jù)圖片url獲取file對象并用axios上傳

    Vue項目如何根據(jù)圖片url獲取file對象并用axios上傳

    這篇文章主要介紹了Vue項目如何根據(jù)圖片url獲取file對象并用axios上傳問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Vue程序化的事件監(jiān)聽器(實例方案詳解)

    Vue程序化的事件監(jiān)聽器(實例方案詳解)

    本文通過兩種方案給大家介紹了Vue程序化的事件監(jiān)聽器,每種方案通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2020-01-01
  • 分析Vue指令實現(xiàn)原理

    分析Vue指令實現(xiàn)原理

    自定義指令是vue中使用頻率僅次于組件,其包含bind、inserted、update、componentUpdated、unbind五個生命周期鉤子。本文將對vue指令的工作原理進(jìn)行相應(yīng)介紹
    2021-06-06
  • Vue中的字符串模板的使用

    Vue中的字符串模板的使用

    本篇文章主要介紹了Vue中的字符串模板的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Vue實現(xiàn)計算器計算效果

    Vue實現(xiàn)計算器計算效果

    這篇文章主要為大家詳細(xì)介紹了Vue實現(xiàn)計算器計算效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • vue-cli 3.0 版本與3.0以下版本在搭建項目時的區(qū)別詳解

    vue-cli 3.0 版本與3.0以下版本在搭建項目時的區(qū)別詳解

    這篇文章主要介紹了vue-cli 3.0 版本與3.0以下版本在搭建項目時的區(qū)別詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • 前端axios取消請求總結(jié)詳解

    前端axios取消請求總結(jié)詳解

    這篇文章主要為大家介紹了前端axios取消請求總結(jié)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • 解析Vue2 dist 目錄下各個文件的區(qū)別

    解析Vue2 dist 目錄下各個文件的區(qū)別

    本篇文章主要介紹了解析Vue2 dist 目錄下各個文件的區(qū)別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • vue中的vue-print-nb如何實現(xiàn)頁面打印

    vue中的vue-print-nb如何實現(xiàn)頁面打印

    這篇文章主要介紹了vue中的vue-print-nb如何實現(xiàn)頁面打印,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04

最新評論