当js里有多个export怎么引入?

// mutation-types.js
export const SOME_MUTATION = 'SOME_MUTATION'
export const SOME_MUTATION_1 = 'SOME_MUTATION_1'

我要import引入mutation-types.js,怎么写比较好?

阅读 10.9k
7 个回答
import * as types from 'mutation-types.js'
// types.SOME_MUTATION types.SOME_MUTATION_1
import * as TYPES from './mutation-types';

console.log(TYPES.SOME_MUTATION)
如果你想全部导入:
import * as mutationTypes from './mutation-types';
如果你只是想导入 mutation-types里的几个可以:

import {SOME_MUTATION,SOME_MUTATION_1} from './mutation-types';

你可以吧SOME_MUTATION和SOME_MUTATION_1放在一个export的对象里面呀:

let obj = {
    SOME_MUTATION : 'SOME_MUTATION',
    SOME_MUTATION_1 : 'SOME_MUTATION_1'
}

export obj

引入
import * as types from '../../mutation-types';
使用
types.SOME_MUTATION
types.SOME_MUTATION_1

import * as types from './types';

然后用 types[导出名] 可以使用

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题