daily-share icon indicating copy to clipboard operation
daily-share copied to clipboard

mobx轻量级状态管理(2020-2-13)

Open yaogengzhu opened this issue 5 years ago • 1 comments

mobx轻量级

最简单的用法

import { observable, action } from 'mobx'
class UserStore {
    @observable
    public userInfo: any = {}

    @observable
    public shopInfo: any = {}

    @observable
    public staffInfo: any = {}

    @observable
    public token = ""

    @action
    public setUserInfo(userInfo: any) {
        this.userInfo = userInfo
    }

    @action
    public setShopInfo(shopInfo: any) {
        this.shopInfo = shopInfo
    }

    @action
    public setStaffInfo(staffInfo: any) {
        this.staffInfo = staffInfo
    }

    @action
    public setToken(token: string) {
        this.token = token
    }

    public canIUse(id: number) {
        if (this.staffInfo && this.staffInfo.permission_list) {
            return this.staffInfo.permission_list.includes(id)
        } else {
            return false
        }
    }
}

export {
    UserStore
}

export const userStore = new UserStore()

yaogengzhu avatar Feb 13 '20 12:02 yaogengzhu

调用

import { userStore } from '@/store/modules/users'
//
userStore.setUserInfo('数据')

yaogengzhu avatar Feb 13 '20 12:02 yaogengzhu