admin管理员组文章数量:1394585
I have a react redux application with typescript. So the scenario is like this, I have an index.cshtml file that includes some javascript.
<script type="text/javascript">
function test() {
alert('Function from index.html');
}
</script>
Now on my react ponent, on Main.tsx in ponentWillMount() function I want to call test function.
ponentWillMount() {
window.test();
}
but this is not working for me. The message is test doesn't exist on type window
Any help is greatly appreciated.
I have a react redux application with typescript. So the scenario is like this, I have an index.cshtml file that includes some javascript.
<script type="text/javascript">
function test() {
alert('Function from index.html');
}
</script>
Now on my react ponent, on Main.tsx in ponentWillMount() function I want to call test function.
ponentWillMount() {
window.test();
}
but this is not working for me. The message is test doesn't exist on type window
Any help is greatly appreciated.
Share Improve this question asked Nov 30, 2018 at 12:49 RapiraRapira 831 silver badge7 bronze badges3 Answers
Reset to default 6You can extend the Window
interface like this:
interface Window {
test(): void;
}
When you are doing this within a module, you need to ensure it is the global Window
interface you are extending:
declare global {
interface Window {
test(): void;
}
}
This provides the type implementation to the piler.
I suggest to use here some Utility class. As I understand you need some functions which you can use in other ponents. So, the best way to do it is to create Util class with statics methods. or some module like this:
export default {
test() {
console.log('foo');
}, ...
};
If you are sure that you have a global variable. You can type your self define file in typing folder.
interface Window {
test: () => void
}
if you use eslint, you should also set test to globals config option.
本文标签: reactjsCall external Javascript function from react typescript componentsStack Overflow
版权声明:本文标题:reactjs - Call external Javascript function from react typescript components - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744097136a2590484.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论