JavaScript变量未定义,尽管它已定义。

huangapple go评论57阅读模式
英文:

Javascript variable is not defined even though it is

问题

我正在运行一个Laravel项目,并在app.js文件的顶部定义了一个名为wmDefaults的对象,如下所示:

var wmDefaults = {
    elemClass: 'waitMe',
    effect: 'bounce',
    text: '',
    bg: 'rgba(255,255,255,0.7)',
    color: '#000',
    maxSize: '',
    waitTime: -1,
    textPos: 'vertical',
    fontSize: '',
    source: ''
};

app.js文件已经包含在JavaScript外部资源列表中(参见下面的截图)。

然而,在尝试在console.log中访问该变量后,我收到了错误消息:

VM2335:1 Uncaught ReferenceError: wmDefaults is not defined
    at <anonymous>:1:13

我已经写JavaScript代码15年了,但我很尴尬地无法解决这个问题。非常感谢任何帮助。

JavaScript变量未定义,尽管它已定义。

JavaScript变量未定义,尽管它已定义。

英文:

I am running a Laravel project and defined an object wmDefaults at the top of the app.js file like this:

var wmDefaults = {
    elemClass:&#39;waitMe&#39;,
    effect : &#39;bounce&#39;,
    text : &#39;&#39;,
    bg : &#39;rgba(255,255,255,0.7)&#39;,
    color : &#39;#000&#39;,
    maxSize : &#39;&#39;,
    waitTime : -1,
    textPos : &#39;vertical&#39;,
    fontSize : &#39;&#39;,
    source : &#39;&#39;
};

The app.js file has been included in the list of javascript external resources (see screenshot below)

JavaScript变量未定义,尽管它已定义。

However, after trying to access the variable in console.log, I get the error:

VM2335:1 Uncaught ReferenceError: wmDefaults is not defined
    at &lt;anonymous&gt;:1:13

Been writing Javascript code for 15 years but I'm embarassed I can't figure this one out. Any help would be really appreciated.

JavaScript变量未定义,尽管它已定义。

答案1

得分: 1

Here is the translated content:

"我找到了解决方法。解决方案在这里:https://stackoverflow.com/questions/50484930/how-to-use-laravel-mix-define-global-javascript-function?rq=4

我需要在捆绑的 JavaScript 文件之外使用它,我必须将它附加到 window 对象上

window.wmDefaults = {
    elemClass: 'waitMe',
    effect: 'bounce',
    text: '',
    bg: 'rgba(255,255,255,0.7)',
    color: '#000',
    maxSize: '',
    waitTime: -1,
    textPos: 'vertical',
    fontSize: '',
    source: ''
};"

请告诉我下一步需要翻译的内容。

英文:

Okay so I figured this out. Solution here: https://stackoverflow.com/questions/50484930/how-to-use-laravel-mix-define-global-javascript-function?rq=4

I needed to use it outside of my bundled JavaScript files I had to attach it to the window object

window.wmDefaults = {
    elemClass:&#39;waitMe&#39;,
    effect : &#39;bounce&#39;,
    text : &#39;&#39;,
    bg : &#39;rgba(255,255,255,0.7)&#39;,
    color : &#39;#000&#39;,
    maxSize : &#39;&#39;,
    waitTime : -1,
    textPos : &#39;vertical&#39;,
    fontSize : &#39;&#39;,
    source : &#39;&#39;
};

huangapple
  • 本文由 发表于 2023年6月13日 15:53:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定