weixin_39583655
2020-12-02 11:11 阅读 220

Uncaught ReferenceError: exports is not defined

Hi to everyone here, I'm new at phaser editor. I code in Typescript. I want to create a class and import it in level.ts. Bu I got "Uncaught ReferenceError: exports is not defined" message at browser's console. My code is this: `///

import { shapeController } from "./ShapeControllerHTMLV1";

class Level extends Phaser.State {

constructor() {

    super();

}

init() {

    this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
    this.scale.pageAlignHorizontally = true;
    this.scale.pageAlignVertically = true;

}

preload () {

    this.load.pack('section0', 'assets/pack.json');

}

create() {
    var a = new shapeController.Greeter(this.game, 10, 10, 'blabla');

}

}And this is my class:export namespace shapeController{ export class Greeter extends Phaser.Sprite{ constructor(gm:Phaser.Game, _x:number, _y:number, _name:string){ super(gm, _x, _y, _name); } }
}`

该提问来源于开源项目:PhaserEditor2D/PhaserEditor

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

9条回答 默认 最新

  • weixin_39902345 weixin_39902345 2020-12-02 11:11
    
    "Uncaught ReferenceError: exports is not defined
    

    Maybe you are using the word exports somewhere, but it should be the keyword export.

    点赞 评论 复制链接分享
  • weixin_39902345 weixin_39902345 2020-12-02 11:11

    I don't see the exports word in your example. Is it in other files? The scene compiler does not generate any export code.

    点赞 评论 复制链接分享
  • weixin_39583655 weixin_39583655 2020-12-02 11:11

    Yes you're right. This is seen at converted js code. `"use strict"; var extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto: [] } instanceof Array && function (d, b) { d.proto = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function () { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true });

    var ShapeControllerHTMLV1_1 = require("./ShapeControllerHTMLV1"); var Level = (function (_super) { __extends(Level, _super); function Level() { return _super.call(this) || this; } Level.prototype.init = function () { this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL; this.scale.pageAlignHorizontally = true; this.scale.pageAlignVertically = true; this.stage.backgroundColor = '#ffffff'; }; Level.prototype.preload = function () { }; Level.prototype.create = function () { this.add.text(344, 292, 'Hello world!', { "font": "bold 20px Arial" }); var a = new ShapeControllerHTMLV1_1.shapeController.Greeter(this.game, 10, 10, 'blabla'); }; return Level; }(Phaser.State));`

    点赞 评论 复制链接分享
  • weixin_39902345 weixin_39902345 2020-12-02 11:11

    So can we close this? Did you find the problem?

    点赞 评论 复制链接分享
  • weixin_39583655 weixin_39583655 2020-12-02 11:11

    No unfortinately I haven't found the problem yet. I add all my classes into my level.ts file. So this solution has many difficulties as you know.

    点赞 评论 复制链接分享
  • weixin_39902345 weixin_39902345 2020-12-02 11:11

    I am in the willing of help. Maybe you can create a small project just to reproduce the bad behavior, and attach it here so I can look in it and try to find the source of the problems.

    点赞 评论 复制链接分享
  • weixin_39583655 weixin_39583655 2020-12-02 11:11

    moduleTrial.zip Hi, Sorry I was very bussy. I attached a simple example project created in Phaser Ediyor 1.4.4

    点赞 评论 复制链接分享
  • weixin_39902345 weixin_39902345 2020-12-02 11:11

    I am looking this. It think when you use import/export the target module system should support it. For example, you should include commonjs in your project. I have not too much experience with TypeScript, let me do a litte more research.

    Thanks for send the project, it is helping a lot.

    点赞 评论 复制链接分享
  • weixin_39902345 weixin_39902345 2020-12-02 11:11

    Ok, look this answer here:

    https://stackoverflow.com/questions/43472778/typescript-exports-is-not-defined

    It is as I said in the last comment. If you want to avoid that exception:

    • Use a third-party module loader.
    • Or don't use any import/export keywords, just keep everything in the global scope.

    Note if you use the second option you have to load all the js files in your index.html file or as script assets in the assets-pack.json file.

    点赞 评论 复制链接分享