我已经在我的laravel项目中使用了这段js代码来获取moment库。
var today = moment();
function Calendar(selector, events) {
this.el = document.querySelector(selector);
this.events = events;
this.current = moment().date(1);
this.draw();
var current = document.querySelector('.today');
if (current) {
var self = this;
window.setTimeout(function () {
self.openDay(current);
}, 500);
}
}
我已经在body部分中使用这个脚本标记调用了js文件
<script src="moment.min.js"></script>
现在获取未捕获的ReferenceError: moment未在控制台中定义。moment.min.js在同一目录中。我该怎么解决这个问题呢?
发布于 2021-07-06 06:55:24
尝尝这个
function loadError(oError) {
throw new URIError("The script " + oError.target.src + " didn't load
correctly.");
}
function affixScriptToHead(url, onloadFunction) {
var newScript = document.createElement("script");
newScript.onerror = loadError;
if (onloadFunction) { newScript.onload = onloadFunction; }
document.head.appendChild(newScript);
newScript.src = url;
}
发布于 2020-08-06 13:00:02
这是因为,由于脚本是异步加载的,所以在将脚本加载到浏览器之前,您正试图访问该时刻。执行此操作的最好方法是检查您正在使用的库是否已加载。您可以通过添加以下链接到该库
function loadError(oError) {
throw new URIError("The script " + oError.target.src + " didn't load correctly.");
}
function affixScriptToHead(url, onloadFunction) {
var newScript = document.createElement("script");
newScript.onerror = loadError;
if (onloadFunction) { newScript.onload = onloadFunction; }
document.head.appendChild(newScript);
newScript.src = url;
}
在你能够引用moment之后,就可以引用https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement了
https://stackoverflow.com/questions/63276473
复制相似问题