我有以下简单的课程。
我想在其导出定义中添加static list
。
// DataService.js
import Api from '@/services/Api'
export default {
// const states = ['ABIA', 'ADAMAWA', 'AKWA IBOM', 'ANAMBRA', 'BAUCHI', 'BAYELSA', 'BENUE', 'BORNO', 'CROSS RIVER', 'DELTA', 'EBONYI', 'EDO', 'EKITI', 'ENUGU', 'FCT(ABUJA)', 'GOMBE', 'IMO', 'JIGAWA', 'KADUNA', 'KANO', 'KATSINA', 'KEBBI', 'KOGI', 'KWARA', 'LAGOS', 'NASARAWA', 'NIGER', 'OGUN', 'ONDO', 'OSUN', 'OYO', 'PLATEAU', 'RIVERS', 'SOKOTO', 'TARABA', 'YOBE', 'ZAMFARA'],
list (entity, p) { return Api().get(entity, {params: p}) },
get (entity, id) { return Api().get(entity + '/' + id) },
create (entity, o) { return Api().post(entity, o) },
update (entity, idField, o) { return Api().put(entity + '/' + o[idField], o) },
delete (entity, id) { return Api().delete(entity + '/' + id) }
}
怎么办呢?
答案 0 :(得分:1)
将states数组从类中取出并将其导出为named export:
import Api from '@/services/Api'
export const states = ['ABIA', 'ADAMAWA', 'AKWA IBOM', 'ANAMBRA', 'BAUCHI', 'BAYELSA', 'BENUE', 'BORNO', 'CROSS RIVER', 'DELTA', 'EBONYI', 'EDO', 'EKITI', 'ENUGU', 'FCT(ABUJA)', 'GOMBE', 'IMO', 'JIGAWA', 'KADUNA', 'KANO', 'KATSINA', 'KEBBI', 'KOGI', 'KWARA', 'LAGOS', 'NASARAWA', 'NIGER', 'OGUN', 'ONDO', 'OSUN', 'OYO', 'PLATEAU', 'RIVERS', 'SOKOTO', 'TARABA', 'YOBE', 'ZAMFARA'];
export default {
list (entity, p) { return Api().get(entity, {params: p}) },
get (entity, id) { return Api().get(entity + '/' + id) },
create (entity, o) { return Api().post(entity, o) },
update (entity, idField, o) { return Api().put(entity + '/' + o[idField], o) },
delete (entity, id) { return Api().delete(entity + '/' + id) }
}
至import州:
import { states } from './modulePath
导入州和班级:
import moduleName, { states } from './modulePath