Blog
首页
文档
收藏
关于
  • 在线转换时间戳 (opens new window)
  • 在线压缩图片 (opens new window)
  • Float-Double转二进制 (opens new window)
  • 文件转Hex字符串 (opens new window)

HiuZing

🍑
首页
文档
收藏
关于
  • 在线转换时间戳 (opens new window)
  • 在线压缩图片 (opens new window)
  • Float-Double转二进制 (opens new window)
  • 文件转Hex字符串 (opens new window)
  • 前端面试题

  • JavaScript

  • Vue2

  • port

  • CSS

  • Node.js

  • JavaScript优化

  • uniapp

  • Mini Program

  • TypeScript

  • 面向对象编程

  • UI组件

  • Plugin

  • Vue3

  • 性能优化

  • Axios

  • 状态管理

    • pinia

      • Pinia 初始
      • Pinia state
      • Pinia 解构store
      • Pinia Actions
        • 同步
        • 异步
        • 多个action互相调用
      • Pinia Getters
      • Pinia API
    • zustand

  • React

  • Mock

  • Icon

  • Template

  • 构建工具

  • 项目规范配置

  • Taro

  • SVG

  • React Native

  • 前端
  • 状态管理
  • pinia
HiuZing
2023-03-27
目录

Pinia Actions

# 同步

直接调用即可

在index.ts

import { defineStore } from 'pinia'
import { Names } from './store-naspace'
export const useTestStore = defineStore(Names.TEST, {
    state: () => ({
        counter: 0,
    }),
    actions: {
        increment() {
            this.counter++
        },
        randomizeCounter() {
            this.counter = Math.round(100 * Math.random())
        },
    },
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

在App.vue














 



<template>
     <div>
         <button @click="Add">+</button>
          <div>
             {{Test.counter}}
          </div>    
     </div>
</template>
 
<script setup lang='ts'>
import {useTestStore} from './store'
const Test = useTestStore()
const Add = () => {
     Test.randomizeCounter()
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 异步

结合async await 修饰

在index.ts

import { defineStore } from 'pinia'
import { Names } from './store-naspace'
// 定义类型
type Result = {
    name: string
    isChu: boolean
}
// 返回Promise,接收一个泛型
const Login = (): Promise<Result> => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve({
                name: '小满',
                isChu: true
            })
        }, 3000)
    })
}
 
export const useTestStore = defineStore(Names.TEST, {
    state: () => ({
        user: <Result>{},
        name: "123"
    }),
    actions: {
        async getLoginInfo() {
            const result = await Login()
            this.user = result;
        }
    },
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

在App.vue













 




<template>
     <div>
         <button @click="Add">test</button>
          <div>
             {{Test.user}}
          </div>    
     </div>
</template>
 
<script setup lang='ts'>
import {useTestStore} from './store'
const Test = useTestStore()
const Add = () => {
     Test.getLoginInfo()
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 多个action互相调用

state: () => ({
    user: <Result>{},
    name: "default"
}),
actions: {
	async getLoginInfo() {
		const result = await Login()
		this.user = result;
		this.setName(result.name)
	},
	setName (name:string) {
		this.name = name;
	}
},
1
2
3
4
5
6
7
8
9
10
11
12
13
14
上次更新: 2024/08/14, 04:14:33
Pinia 解构store
Pinia Getters

← Pinia 解构store Pinia Getters→

最近更新
01
React Native 使用SVG
08-13
02
Docker基础命令
08-04
03
算数逻辑单元
07-30
更多文章>
Theme by Vdoing | Copyright © 2021-2024 WeiXiaojing | 友情链接
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式