Jon*_* Re 3 javascript import typescript
嘿,我刚开始使用 TypeScript,我尝试将 javascript 复制到 TypeScript 并将内容更改为更加面向对象。现在我正在尝试使用 cmd 和 ns-node 命令启动它。
private username:string;
^
TypeError: Object prototype may only be an Object or null: undefined
Run Code Online (Sandbox Code Playgroud)
这个错误没有任何意义,所以我用谷歌搜索了一下,发现问题可能出在项目的导入上。我已经在不同的网站上尝试了每一个灵魂,但他们没有帮助。
所以我的代码:
import { Entity } from './Entity';
import { Bullet } from './Bullet';
import * as fs from "fs";
export class Player extends Entity{
private username:string;
protected pressingRight:boolean ;
protected pressingLeft:boolean ;
...
}
Run Code Online (Sandbox Code Playgroud)
那么可能是什么问题呢?你的帮助
编辑:
import {Point} from "./Point";
import {Bullet} from "./Bullet";
import {Player} from "./Player";
export class Entity{
protected x:number;
protected y:number;
protected spdX:number = 0;
protected spdY:number = 0;
protected id:number;
protected map:string;
protected static initPack = {player:[],bullet:[]};
protected static removePack = {player:[],bullet:[]};
constructor(x:number,y:number,id:number,map:string){
this.x = x;
this.y=y;
this.id=id;
this.map=map;
}
...}
Run Code Online (Sandbox Code Playgroud)
和 Bullet.ts :
import {Entity} from './Entity';
import {Player} from './Player';
export class Bullet extends Entity{
private angle:number;
private parentID:number;
private timer:number = 0;
private toRemove:boolean=false;
public static list={};
constructor(x:number,y:number,map:string,parentId:number,angle:number,id:number){
super(x,y,id,map);
this.angle = angle;
this.spdX= Math.cos(angle/180*Math.PI) * 10;
this.spdY= Math.sin(angle/180*Math.PI) * 10;
this.parentID = parentId;
Bullet.list[this.id] = self;
Bullet.initPack.bullet.push(this.getInitPack());
}
Run Code Online (Sandbox Code Playgroud)
你有循环导入。根据您从哪个文件开始,当 ts-node 到达一个已经在加载过程中的文件的导入时,即使导入的文件没有完成加载,它也会继续通过该导入。这就是您可以通过import { Entity } from './Entity';并且Entity仍然可以未定义的方式。
如果在加载文件时Entity.ts不需要访问Player该类,请尝试将该import { Player } from './Player';行移动到Entity.ts.
| 归档时间: |
|
| 查看次数: |
10672 次 |
| 最近记录: |