【问题标题】:Meteor: importing whole module doesn't workMeteor:导入整个模块不起作用
【发布时间】:2018-04-13 03:41:10
【问题描述】:

我正在尝试使我的 Meteor 代码与 ES6 兼容。我有一个名为 /both/global.js 的文件,其中包含我希望全局访问的函数和常量。出于 ES6 的目的,我将其移至 /both/imports/global.js,并在所有函数和常量前加上 export const

我还没有改变整个目录结构。模板 javascript 文件仍在 /client/controller/ 中。我已经为导出的函数和常量添加了import 语句。当我以以下形式编写导入语句时: import { fn1, fn2, ... } from '../../both/imports/global.js'; 他们工作正常。

我宁愿用一个单一的导入所有函数和常量: import from '../../both/imports/global.js'; 但这似乎没有任何作用。

我做错了什么?

【问题讨论】:

    标签: javascript meteor import module


    【解决方案1】:

    这与导入和导出的工作方式有关。

    你需要的是

    import * from '/both/imports/global.js'

    或者...

    import something from '/both/imports/global.js'

    将查找default 导出,并将其分配给名为something 的变量。

    import { fn1, fn2, ... } ...

    将命名变量导入你的命名空间

    另一种方法是这样做:

    import globalFunctions from '/both/imports/global.js'

    那么你的函数可以像这样调用:

    globalFunctions.fn1() globalFunctions.fn2()

    【讨论】:

    • 好的,谢谢。那么import from '../../both/imports/global.js'; 是做什么的呢?
    • 什么都不做
    • @Mikkel 最后一个应该是import * as globalFunctions from 'path',否则你仍然只寻找默认导出。
    • 忘记了,添加了它
    猜你喜欢
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    相关资源
    最近更新 更多