typescript下,mobx的store怎么写呢?

发布于 2022-09-11 17:47:34 字数 2286 浏览 17 评论 0

方案A:

目录结构:

store
  |--Auth
       |--index.ts
       |--interface.ts
  |--Bank
       |--index.ts
       |--interface.ts

Auth/interface.ts:

import {IResponse} from '../../util/fetch';

export interface IAuthStore {
  isLogin: boolean,
  loginWithNameAndPw: (params: { username: string, password: string }) => Promise<IResponse>,
  sendMsgCode: (params: { mobile: string }) => Promise<IResponse>,
}

Auth/index.ts:

import {observable, action} from 'mobx';
import {post} from '../../util/fetch';
import {IAuthStore} from './interface'

class Auth implements IAuthStore{
  @observable
  isLogin = false;

  async loginWithNameAndPw({username, password}) {
    try {
      const response = await post(`${userServer}/authz/oauth/token`);
      return response;
    } catch (e) {
      console.log(e);
      return e;
    }
  }

  async sendMsgCode({mobile}){
    try {
      const response = await post(`${userServer}/authz/sms/send`);
      return response;
    } catch (e) {
      console.log(e);
      return e;
    }
  }

}

const authStore = new Auth();
export default authStore;

方案B:

目录结构:

store
  |--Auth
       |--index.ts
  |--Bank
       |--index.ts

Auth/index.ts:

import {observable, action} from 'mobx';
import {post} from '../../util/fetch';
import {IResponse} from '../../util/fetch';

class Auth {
  @observable
  isLogin:string = false;

  async loginWithNameAndPw({username, password}: {username: string, password: string}): Promise<IResponse> {
    try {
      const response = await post(`${userServer}/authz/oauth/token`);
      return response;
    } catch (e) {
      console.log(e);
      return e;
    }
  }

  async sendMsgCode({mobile}: {mobile: string}): Promise<IResponse> {
    try {
      const response = await post(`${userServer}/authz/sms/send`);
      return response;
    } catch (e) {
      console.log(e);
      return e;
    }
  }

}

const authStore = new Auth();
export default authStore;

请问class的成员的类型应该和class分开(方案A),还是和class写在一起(方案B),还是我这两种都没写对?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两个我 2022-09-18 17:47:34

方案B就可以了,不建议分开

掀纱窥君容 2022-09-18 17:47:34

可以分开,我会选择方案A

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文