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),还是我这两种都没写对?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
方案B就可以了,不建议分开
可以分开,我会选择方案A