【问题标题】:Uncaught (in promise) ReferenceError converting addEventListener to Typescript未捕获(承诺中)ReferenceError 将 addEventListener 转换为 Typescript
【发布时间】:2019-01-08 18:36:18
【问题描述】:

我开发了一个简单的网页,用于将文本输入发送到 XML RPC 服务器。我得到了在 Javascript 中工作的代码,但现在我试图将其全部转换为 Typescript 并使用带有 async/await 的 Promise。这是一个学习实验。事实证明,以我目前的经验水平,这有点困难。

以前工作的 Javascript 代码:

import "./mimic.js";
function makeComment() {
    const method = "MakeComm";
    let request = new XmlRpcRequest("http://localhost:1337/RPC2", method);
    request.addParam(document.getElementById("n1")).value;
    request.addParam(document.getElementById("n2")).value;
    let response = request.send();
    console.log(response);
}

正如我上面所说,这个函数与 XML RPC 服务器正确通信。这是我转换后的 Typescript 代码:

import "./mimic.js";
const updateCommentBtn: HTMLButtonElement = document.getElementById(
    'makeComment',) as HTMLButtonElement;

updateCommentBtn.addEventListener('click', async () => {
    const method = "MakeComm";
    let request:any = new XmlRpcRequest("http://localhost:1337/RPC2", method);
    request.addParam(<HTMLInputElement>document.getElementById("n1")).value;
    request.addParam(<HTMLInputElement>document.getElementById("n2")).value;
    let response = await request.send();
    console.log(response);
});

我收到一个错误:

Uncaught (in promise) ReferenceError: XmlRpcRequest is not defined
    at Object.<anonymous> (fileChange.ts:36)
    at new Promise (<anonymous>)
    at HTMLButtonElement.<anonymous> (fileChange.ts:34)

XmlRpcRequest 调用是一个包含在 ./mimic.js 中的函数。

我的 Typescript 版本的 HTML:

<p>Make Comment:</p>
<input type="text" id="n1"/>
<input type="text" id="n2"/>
<button id="makeComment">Update Comment</button>

编辑: mimic.js 对 XmlRpcRequest 的定义如下:

function XmlRpcRequest(url, method) {
    this.serviceUrl = url;
    this.methodName = method;
    this.crossDomain = false;
    this.withCredentials = false;   
    this.params = [];
    this.headers = {};
};

【问题讨论】:

  • request.send() 返回什么?
  • XmlRpcRequest 是在mimic.js 中导出的吗?也许从 "./mimic.js" 导入 {XmlRpcRequest} ?
  • XmlRpcRequest 没有出现在mimic.js 中导出。 (它是前一段时间开发的)。我得到文件“.../mimic.js”不是模块。
  • @DanielA.White 我没有从 request.send 得到任何输出。在此之前它会因异常而失败。
  • XmlRpcRequest 位于何处?很明显,它在脚本上下文中不可见,因此您应该检查它的位置并使其对脚本可见。

标签: javascript typescript xml-rpc


【解决方案1】:

感谢您添加代码

你应该像这样在mimic.js中导出函数

export const XmlRpcRequest = (url, method) => {
    this.serviceUrl = url;
    this.methodName = method;
    this.crossDomain = false;
    this.withCredentials = false;   
    this.params = [];
    this.headers = {};
};

然后导入

import { XmlRpcRequest } from "./mimic";

如果你的 typescript linter 给你 gyp,你可以像这样为参数添加定义:

  export const XmlRpcRequest = (url: string, method: string) => {
        this.serviceUrl = url;
        this.methodName = method;
        this.crossDomain = false;
        this.withCredentials = false;   
        this.params = [];
        this.headers = {};
  };

【讨论】:

  • 我已经更新了主帖,介绍了mimic.js 如何定义XmlRpcRequest
  • 看起来像是打字稿问题而不是 javascript 问题。您将如何解决此类问题?
  • 这成功了!有趣的是,我不得不先重新启动 VS Code……它仍然在抱怨环境。现在它指向 request.addParam 原型(未定义)的类似问题。我想我也需要将其定义为“export const”?
  • 它的语法有点不同... XmlRpcRequest.prototype.addParam=function(...)
  • 我应该说,上面的编译但在控制台日志中提供了一个错误。 “未捕获的 TypeError:无法设置未定义的属性 'addParam'”
猜你喜欢
  • 2014-06-05
  • 2020-02-29
  • 2021-11-08
  • 2021-01-12
  • 1970-01-01
  • 2017-11-24
  • 2018-01-31
  • 2023-02-21
  • 2023-02-25
相关资源
最近更新 更多