我知道这种问题已经被问了很多次了。
对不起,我是React JS,JavaScript等的新手。并且我正在为此项目使用React应用程序工具。
我正在准备登录表格。因此,当用户单击登录按钮时,功能很简单。函数应该被调用。
我的函数在src / xyz中,我希望它从我的index.html中调用。但是失败了。并给予例外
公共
src
//my code in src/xyz.js
function testing(){
console.log("welcome user")
}
//this code works which is in same directory
// public/see.js
function hello(){
console.log("hi")
}
<!DOCTYPE html>
<html lang="en">
<head>
<script type = "text/javascript" src="/src/index.js"></script>
<script type = "text/javascript" src="./see.js"></script>
<script type = "text/javascript" src="/src/xyz.js"></script>
</head>
<body>
<div class="root">
<h1>Login form</h1>
<table id=login>
<tr>
<td>UserName</td>
<td><input type="email" /> </td>
</tr>
<tr>
<td>password</td>
<td><input type="password" /></td>
</tr>
<tr>
<td></td>
<td>
<button type="submit" id="login" onclick="testing()">Log In</button>
</td>
</tr>
</table>
</div>
</body>
</html>
答案 0 :(得分:0)
来自您的src属性,请注意,对于see.js,您拥有“ ./”,而只有“ / src / xyz” 您应该将其更改为“ ./src/xyz.js”
<script type = "text/javascript" src="./see.js"></script>
<script type = "text/javascript" src="/src/xyz.js"></script>
答案 1 :(得分:-1)
首先,您知道react中的react结构,您无需将js文件调用到public / index.html中,而是使用src / index.js文件访问所有内容。 所有文件导入src / index.js