所以我有一个文件,它包含一个函数
const registerUsernameValidation = (username, n_patt) => {
if (username.val() == "" || username.val().length > 12 || username.val().length < 3) {
username.addClass("border-danger");
$("#u_error").html("<span class='text-danger'>Please Enter A Username</span>");
status = false;
} else if (username.val().length > 12) {
username.addClass("border-danger");
$("#u_error").html("Please Enter A Username With A Length Less Than 12");
status = false;
} else if (username.val().length < 3) {
username.addClass("border-danger");
$("#u_error").html("Please Enter A Username With A Length Greater Than 3");
status = false;
} else if (!n_patt.test(username.val())) {
username.addClass("border-danger");
$("#u_error").html("Please Enter A Valid Username, Only Letters And Numbers");
status = false;
} else {
username.removeClass("border-danger");
$("#u_error").html("");
status = true;
}
}
export {registerUsernameValidation};
我的 main.js:
import { registerUsernameValidation } from "./validation.js";
$(document).ready(function() {
$("#register_form").on("submit", function() {
var username = $("#username");
var email = $("#email");
var pass1 = $("#password1");
var pass2 = $("#password2");
var type = $("#usertype");
var n_patt = new RegExp(/^(?![0-9]*$)[a-zA-Z0-9]+$/);
var e_patt = new RegExp(/^[a-z0-9_-]+(\.[a-z0-9_-]+)*@[a-z0-9_-]+(\.[a-z0-9_-]+)*(\.[a-z]{2,4})$/);
registerUsernameValidation(username, n_patt);
});
});
出于某种原因,我仍然收到错误消息 未捕获的语法错误:不能在模块外使用导入语句
答案 0 :(得分:1)
将 "type": "module"
添加到您的 package.json。这将告诉节点 import
将用于导入文件而不是 require
。
将 type="module"
属性添加到您的 <script>
标签。