方案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),还是我这两种都没写对?
方案B就可以了,不建议分开