gpt4 book ai didi

javascript - TypeScript 定义外部类

转载 作者:行者123 更新时间:2023-11-28 19:18:51 25 4
gpt4 key购买 nike

目前我的代码如下所示:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com