TypeScript 0.9.1.1을 사용하면 다른 모듈/파일에서 정적 변수에 액세스 할 때 정의되지 않습니다.TypeScript : RequireJS에서 정적 변수 사용 AMD
예 번호 :
App.ts :
import Game = require('Game');
var game = new Game();
Game.ts :
import Grid = require('Grid');
class Game
{
public Grid: Grid;
public static Game: Game;
constructor()
{
Game.Game = this;
this.Grid = new Grid();
this.Grid.SeeIfStaticWorks();
}
}
export = Game;
Grid.ts :
0,123,824,221,418을하지만 SeeIfStaticWorks()
내부에서 액세스를 시도 할 때 정의되지 않는다 :
질문은 다음과 같습니다 this.Grid.SeeIfStaticWorks();
를 호출하기 전에 Game.Game
을 검사는
가 정의되는 방법을 보여줍니다 다른 모듈에서 정적 변수에 액세스 할 수 있습니까? 나도 같은 논리를 사용하는 경우
class Grid
{
public SeeIfStaticWorks()
{
console.log(Game.Game);
if (Game.Game)
alert('Instance is defined!');
else
alert('Instance is undefined!');
}
}
class Game
{
public Grid: Grid;
private static game : Game;
public static get Game() : Game
{
if (this.game == null)
{
this.game = new Game();
}
return this.game;
}
constructor()
{
this.Grid = new Grid();
}
}
var game = Game.Game;
game.Grid.SeeIfStaticWorks();
는 AMD가 RequireJS와 함께 :
업데이트 : 모듈에서 정적 변수 (demo here)를 사용하여 허용 하나 개의 파일에서 모든 코드를 실행
SeeIfStaticWorks()
을 호출 할 때 정적 변수가 정의되지 않음 :
App.ts :
import Game = require('Game');
var game = Game.Game;
game.Grid.SeeIfStaticWorks();
Game.ts :
import Grid = require('Grid');
class Game
{
public Grid: Grid;
private static game : Game;
public static get Game() : Game
{
if (this.game == null)
{
this.game = new Game();
}
return this.game;
}
constructor()
{
this.Grid = new Grid();
}
}
export = Game;
Grid.ts이 그 때문에 파일 Game.ts
이 Game.Game
을 구문 분석됩니다
import Game = require('Game');
class Grid
{
public SeeIfStaticWorks()
{
console.log(Game.Game);
if (Game.Game)
alert('Instance is defined!');
else
alert('Instance is undefined!');
}
}
export = Grid;
'SeeIfStaticWorks()'가'Game.Game'이 인스턴스화되기 전에 실행되기 때문에 그렇게 작동하지 않습니다. 내가 사용하는 것에 더 가까운 코드로 질문에 대한 업데이트를 게시했습니다. RequireJS와 관련된 문제가있는 것 같습니다. 좋은 AngularJs/TS 비디오 btw :) –
그것은이 방법으로 작동하지 않으므로, 제발 대답을 삭제할 수 있습니까? 그러면 정답을 더 빨리 얻을 수 있습니다. –
다른 답변보기 – basarat