使用webpack打包后找不到全局变量,如何才能解决?

地址

var BTY_CONFIG = (function () {
    var config = {
        planet: [{
            name: '电子',
            link: 'https://www.baidu.com/?电子'
        }, {
            name: '大数据',
            link: 'https://www.baidu.com/?数据'
        }],
        share: [,
            '霍尔拖',
            '发区'
        ],
        collection: ['数据共享交换', '信用信息共享']
    };
    return config;
}());
require('./config.js');

console.log(BTY_CONFIG.planet[1].name)

图片描述

/*
var BTY_CONFIG = (function () {
    var config = {
        planet: [{
            name: '电子',
            link: 'https://www.baidu.com/?电子'
        }, {
            name: '大数据',
            link: 'https://www.baidu.com/?数据'
        }],
        share: [,
            '霍尔拖',
            '发区'
        ],
        collection: ['数据共享交换', '信用信息共享']
    };
    return config;
}());
*/

const BTY_CONFIG = {
    planet: [{
        name: '电子',
        link: 'https://www.baidu.com/?电子'
    }, {
        name: '大数据',
        link: 'https://www.baidu.com/?数据'
    }],
    share: [,
        '霍尔拖',
        '发区'
    ],
    collection: ['数据共享交换', '信用信息共享']
};

export default BTY_CONFIG
// require('./config.js');

// console.log(BTY_CONFIG.planet[1].name)

import BTY_CONFIG from './config.js'

console.log(BTY_CONFIG.planet[0].name)
阅读 7.1k
2 个回答

BTY_CONFIG是模块的内部变量,你没有导出, 使用

export BTY_CONFIG
// 导入时候
import {BTY_CONFIG} from './config.js'

或者

export default BTY_CONFIG    // 这样写导入时候就可以取其他名字
// 导入时候
import Config = from './config.js'

或者

module.exports = BTY_CONFIG

反正你要把这个变量导出来

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