0

我正在以函数表示法实现 pinia 商店,并且想知道如何声明 getter 和操作。

这些是作为简单功能实现的,没有区别吗?

import { defineStore } from 'pinia'
import HttpService from '../services/BaseHttpService';
import { ref } from 'vue'
import { User } from '../models/user.model';

const httpService = new HttpService('/users');
    
export const useUserStore = defineStore('user', () => {
    const authedUser = ref(new User())
    const users = ref<User[]>([])

    function authed() {
        return typeof authedUser.value['@id'] === "string";
    }

    function login(data: { email: string, password: string }) {
        const http = new HttpService('/login');
        return http.post(data).then((response) => {
            authedUser.value = response
            users.value = [...users.value, response]
        })
    }

    function getter() {
        return users.value;
    }

    function action(id: number) {
        httpService.get(id).then((response) => {
            users.value = [...users.value, response]
        })
    }

    return {authedUser, users, authed, login}
})
4

0 回答 0