typescript下,mobx的store怎么写呢?

方案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),还是我这两种都没写对?

阅读 3.8k
2 个回答

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

新手上路,请多包涵

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进