繁体   English   中英

通过Webpack捆绑的Expose模块

[英]Expose module bundled through Webpack

我对使用Webpack相当陌生,并且正在尝试将其合并到现有的电子商务网站中。 我有一个像这样的简单类,但是我需要能够在html的脚本标签内访问其方法。 目前,我不确定,将其公开给整个应用程序的适当方法是什么?

//account.js
class Account {
    constructor() {
        this.transactions = [];
    }

    deposit( amount, date ) {
        this.transactions.push({ 
            amount : amount,
            date   : date
        });
    }
};

export default Account;


//app.js
import Account from './account';
const account = new Account();

到底是什么未定义? 请发布您的webpack.config.js和package.json-错误可能位于此处。

通常,您必须将要使用的函数或类导出到其他地方,然后再导入到那里。

export function deposit() {..}

import {deposit as depo} from "./path-to/account"

您是否尝试过以下方法之一?

modules.export = account;

const account = require('account.js');

我还建议查看Webpack导出加载器以导出全局变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM