【问题标题】:Browserify global variable is not found in the browser在浏览器中找不到 Browserify 全局变量
【发布时间】:2023-07-20 04:27:01
【问题描述】:

我正在使用 TypeScript 编写脚本,当我使用 browserify 进行转换时,在浏览器中找不到我的主变量。

ma​​in.ts

import { GameSmart } from './GameSmart';

const gamesmart = new GameSmart();

gulpfile.js

/* require's snipped */

gulp.task('build', function () {
    return browserify()
        .add('./src/gamesmart/main.ts')
        .plugin(tsify)
        .bundle()
        .on('error', function (error) {
            console.error(error);
            throw error;
        })
        .pipe(source('gamesmart.js'))
        .pipe(gulp.dest('build/'));
});

在我的页面中,我包含了新创建的文件,并尝试调用 gamesmart 变量,但未找到。

<script scr="/path/to/sdk.js">
<script>
    gamesmart.game.started();
</script>

如何将其设为全局/根变量?

【问题讨论】:

    标签: javascript typescript browserify tsify


    【解决方案1】:

    我可以通过将变量添加到 window 来解决此问题。

    window['gamesmart'] = new GameSmart();
    

    【讨论】: