简体   繁体   English

打字稿在工作代码上引发错误

[英]Typescript throwing error on working code

I'm implementing Typescript into an example project to practice the use of webpack, ES6, npm... 我正在将Typescript实施到示例项目中,以练习使用webpack,ES6,npm ...

I have loaders 'awesome-typescript-loader', 'babel-loader' which are functioning fine as the code is being bundled and is running. 我有加载器“ awesome-typescript-loader”,“ babel-loader”,它们在代码被捆绑并正在运行时运行良好。

The problem is that when i test something like this - 问题是当我测试这样的东西时-

document.getElementById('test-id').innerHTML = 'asdf';

I get an error in the browser console. 我在浏览器控制台中遇到错误。

打字稿浏览器控制台错误

However, the code is working, and the ID i am specifying is having that 'asdf' string injected. 但是,代码正在运行,并且我指定的ID已插入了“ asdf”字符串。

I have tried alternate loader 'ts-loader', using querySelector() instead of getElementById() ... also assigning the element to a variable, then trying to set the .innerHTML , but to no avail. 我已经尝试过使用querySelector()而不是getElementById()替代加载器'ts-loader'...还将元素分配给变量,然后尝试设置.innerHTML ,但无济于事。

The resolution to this problem, as casraf guessed, was that the script was running twice, once before + once after the DOM had been loaded. 正如casraf猜想的那样,解决该问题的方法是脚本运行了两次,一次是在DOM加载之前,另一次是+。

In this case i was using html-webpack-plugin to handle injecting script links, and still had a hardcoded copy in my index.html. 在这种情况下,我使用html-webpack-plugin来处理注入脚本链接,但在index.html中仍然有一个硬编码的副本。 It was solved by removing the hardcoded link. 通过删除硬编码链接解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM