【问题标题】:Import Resource Bundle to vue project将资源包导入 vue 项目
【发布时间】:2018-07-21 16:01:32
【问题描述】:

我想将包 ResourceBundle 添加到我的 vue 项目中。所以我安装了它:

npm install resource-bundle

这些是 index.js 资源包中的导出部分:

export function* loader (locale, dir, baseName) {
    console.log('in loader')//Does not print
    ...
    return new ResourceBundle(require(file))
};

function ResourceBundle (resource) {
    this.resource = resource
}

ResourceBundle.prototype.get = function (key) {
    ...
    return util.format.apply(util, params);
};

export const ResourceBundlee = ResourceBundle

然后在main.js中导入并使用'loader':

import * as ResourceBundle from 'resource-bundle'
console.log(ResourceBundle)
//console:
// ResourceBundlee:ƒ s(t) 
// loader:ƒ r(t,e,n)  
console.log(ResourceBundle.loader('en_US', 
__dirname+'/resources',message').get) 
//console:
//undefined

我不知道 javascript。您能解释一下发生了什么,为什么不打印 'in loader' 以及我应该如何以正确的方式导入 loader?谢谢

【问题讨论】:

    标签: javascript ecmascript-6 vue.js vuejs2 es6-modules


    【解决方案1】:

    npm install ResourceBundle 应该是 npm install --save resource-bundle。尝试使用正确的包名和保存标志重新安装。

    之后,检查项目根目录中的package.json 文件中的resource-bundle 是否列在"dependencies" 部分中。

      "dependencies": {
        ...
        "resource-bundle": "^0.1.2",
        ...
      }
    

    将您的导入语句更改为:

    import loader from 'resource-bundle'
    

    并调用加载函数:

    let message = loader('en_US', __dirname+'/resources',message')
    console.log(message.get('some-key')) 
    

    【讨论】:

    • 对于我的问题中的错误,我深表歉意。我实际上已经正确安装了它。现在在问题中编辑命令
    • 我的问题是将这个包导入到 main.js 文件中。
    • 从'resource-bundle'导入加载器,我得到一个'未定义'的加载器
    • 现在查看source on GitHub,它看起来与您发布的内容不同。我没有看到 loader 函数。我看错包了吗?
    • 根据 ECMAScript 6 模块语法,导入对象我必须导出它。所以我以这种方式更改了资源包的 index.js。
    猜你喜欢
    • 2020-11-17
    • 1970-01-01
    • 2011-06-12
    • 2020-08-16
    • 2020-12-02
    • 2020-06-25
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多