假设我在渲染之前在 index.js 文件中用 reactjs 编写了一个测试函数
function testfunction(){
console.log("this is test function written in reactjs")
}
我想在 html 脚本中调用上面的 reactjs 函数。
<html>
<body>
//including reactjs minified js in the index.html
<script src="reactjs.min.js"></script>
<script>
//here i want to call function written in reactjs file
testfunction();
</script>
</body>
</html>
最佳答案
这是丑陋且糟糕的做法,但您可以将函数设置为全局(窗口)上下文,以便其他脚本可以调用它:
window.testfunction = () => console.log("this is test function written in reactjs");
然后,页面上有权访问 window
的其他脚本将能够调用它。
关于javascript - 我如何通过外部javascript调用用reactjs编写的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49376636/