简体   繁体   中英

Expose module bundled through Webpack

I am fairly new to using Webpack and I am trying to incorporate it into an existing e-commerce website. I have a simple class like so but I need to be able to access its methods within a script tag in the html. Currently I get undefined, what is the appropriate way to expose it to the entire app?

//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();

What exactly is undefined? Please post your webpack.config.js and the package.json - the error might be located there.

In general, you have to export your functions or classes you want to use elsewhere and import it there.

export function deposit() {..}

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

Have you tried one of the following?

modules.export = account;

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

I also recommend looking into the Webpack exports-loader to export a global variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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