目前我的代码如下所示:
module Nexus {
export class Scraper {
private summonerName: string;
private apiKey: string = '';
private summonerStatsUrl = '';
constructor(name: string) {
this.summonerName = name;
}
getSeasonRank(): string {
return 'aa';
}
getRankedStats(): string {
return 'aa';
}
getSummonerStats(callback: Function) {
var summonerStats = request(this.summonerStatsUrl + this.apiKey, function (error, body, response) {
callback(response);
});
}
}
}
和app.ts:
///<reference path="./Nexus.ts"/>
var colors = require('colors'),
request = require('request'),
fs = require('fs'),
readline = require('readline'),
rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Insert summoner name: \r\n >> ', function (answer) {
var scraper = new Nexus.Scraper(answer);
scraper.getSummonerStats(function (result) {
console.log(result);
});
});
当我到达新的 Nexus.Scraper()
时,我收到此错误:
Nexus 未定义
虽然应该是因为我包含了它?该模块名为 Nexus,我正在导出 Scraper 类。 (该文件名为 Nexus.ts。)
最佳答案
确保您的模块如下所示:
module Nexus {
export class Scraper {
private summonerName: string;
private apiKey: string = '';
private summonerStatsUrl = '';
constructor(name: string) {
this.summonerName = name;
}
getSeasonRank(): string {
return 'aa';
}
getRankedStats(): string {
return 'aa';
}
getSummonerStats(callback: Function) {
var summonerStats = request(this.summonerStatsUrl + this.apiKey, function (error, body, response) {
callback(response);
});
}
}
}
export = Nexus;
然后,不要使用 /// <reference />
这样做:
import Nexus = require('Nexus');
关于javascript - TypeScript 定义外部类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29282394/