Type
export type UseMethodItem = { name: string; args?: any[]; fallback?: (() => any) | string };
Mixin
import Vue from 'vue';Vue.mixin({ methods: { useMethod(methodItem): void { if (!methodItem) { return; } const { name: methodName, args = [], fallback: fallback }: UseMethodItem = methodItem; if (!methodName) { return; } if (methodName in this) { //@ts-ignore this[methodName].apply(this, [...args]); return; } if (fallback) { try { if (typeof fallback === 'string') { // @ts-ignore this[fallback].apply(this); return; } else { fallback.apply(this); } } catch (e) { this.$logger(`[ UseMethod ] No method ${methodName}() and fallback ${fallback}() on instance.`); return; } } this.$logger(`[ UseMethod ] No method ${methodName}() on instance.`); } }});
ComponmentPage
export default Vue.extend({ data(): DataType { return { config: { featuredButtons: [ { name: '每日签到', icon: 'gift' }, { name: '金币商城', icon: 'shop' } ], settings: [ { name: '账号管理', icon: 'user', options: [ { name: '修改昵称', path: '/pages/user/nickname' }, { name: '修改密码', path: '/pages/user/password' }, { name: '修改头像', path: '' }, { name: '删除账号', path: '' }, { name: '认证指纹', path: '' } ] }, { name: '身份管理', icon: 'identity', options: [ { name: '切换身份', path: '' }, { name: '新增身份', path: '' }, { name: '上传人脸识别照片', path: '' } ] }, { name: '服务管理', icon: 'list', options: [ { name: '查看订单', path: '' }, { name: '查看用户协议', path: '' }, { name: '查看隐私协议', path: '' }, { name: '设置隐私权限', path: '' } ] }, { name: '邀请好友', icon: 'share' }, { name: '退出登录', icon: 'exit', method: { name: 'toLogout', fallback: 'toPromptUnderConstruction' } } ] } }; }, methods: { toPromptUnderConstruction() { this.$toast('暂不可用'); }, gotoSubPage(path: string) { uni.navigateTo({ url: path, fail: () => this.$toast('暂不可用') }); return; }, toLogout() { uni.showModal({ title: '确认提示', content: '退出登录当前账号?', cancelText: '取消', confirmText: '确认', success: (res) => { if (res.confirm) { this.$ewsedu().Auth.logout(); } } }); } }});