BUG YII2.0 $ is not defined

Posted AttitudeY

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BUG YII2.0 $ is not defined相关的知识,希望对你有一定的参考价值。

BUG描述:$ is not defined 没有加载jquery成功

原因:Yii2.0将JS代码默认加载页面加载后

解决方案:

第一种方案:最简单方法是在 assets\AppAsset.php 中加上,页面前加载

public $jsOptions = array(
    ‘position‘ => \yii\web\View::POS_HEAD
);

第二种方案:But in production you usually want the scripts to load last, and instead you let Yii2 handle your javascript:

$this->registerJs(
    ‘$("document").ready(function(){ alert("hi"); });‘
);

Now Yii will handle this js and place it after anything important (like jQuery).

You‘ll however soon notice that IDE‘s are usually bad at handling this kind of language nesting (JavaScript inside PHP) so the syntax highlighting is likely to break. One way to solve it is to register your script in a separate file:

$this->registerJsFile( ‘myScript.js‘ );

If you want even more control about which order to load your scripts, you can add dependencies as your second argument, and additional options as the third:

$this->registerJsFile( 
    ‘myScript.js‘, 
    [‘\backend\assets\AppAsset‘],  
    [‘position‘ => ‘\yii\web\View::POS_END‘]
);

If you for some reason absolutely want the script to be rendered inline you can do:

$this->registerJs( $this->renderPartial(‘myScript.js‘) );

The recommended way to add your scripts is to use AssetBundles. Look in assets/AppAssets.php and add your js-file to the $js-array.

以上是关于BUG YII2.0 $ is not defined的主要内容,如果未能解决你的问题,请参考以下文章

找bug - Target runtime Apache Tomcat v7.0 is not defined

BUG YII2.0 cURL error 6: Could not resolve host:

开发中遇到的bug-Property or method “xxxx“ is not defined on the instance but referenced during render.

nodejs出现require is not defined和__dirname is not define 错误

火狐 event is not defined

调用js报错 错误: $ is not defined