[英]I am getting 'Uncaught Reference Error: moment is not defined' error. How can I fix this?
I have used this js code in my laravel project for getting moment library.我在我的 laravel 项目中使用了这个 js 代码来获取矩库。
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);
}
}
I have called the js file using this script tag inside body section我在正文部分使用此脚本标记调用了 js 文件
<script src="moment.min.js"></script>
Now getting Uncaught ReferenceError: moment is not defined in console.现在得到 Uncaught ReferenceError: moment is not defined in console。 moment.min.js is in the same directory.
moment.min.js 位于同一目录中。 How can I fix this.`
我该如何解决这个问题。
Try this尝试这个
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;
}
This is because, As the script is loaded asynchronously and before loading the script into the browser you are trying to access the moment.这是因为,由于脚本是异步加载的,并且在将脚本加载到浏览器之前,您正试图访问该时刻。 The best way of doing this is to check if the library you are using is loaded or not you can to this by adding something link this
最好的方法是检查您正在使用的库是否已加载,您可以通过添加一些链接来检查这个
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;
}
and after you will be able to refer moment refer https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement在您可以参考之后,请参考https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.