如何将常量列表导出为JavaScript类的一部分

时间:2017-10-18 11:08:47

标签: javascript ecmascript-6 export

我有以下简单的课程。

我想在其导出定义中添加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) }

    }

怎么办呢?

1 个答案:

答案 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