简体   繁体   English

ReferenceError:$未定义yii2

[英]ReferenceError: $ is not defined yii2

Adding a javascript inside my view results in the ReferenceError: $ is not defined . 在我的视图中添加javascript会导致ReferenceError: $ is not defined I assume that the problem is due to Yii2 injects scripts last on my page. 我认为问题是由于Yii2在我的页面上最后注入脚本。 How to fix this? 如何解决这个问题?

Or how will I prevent Yii2 from autoloading the script files? 或者我如何阻止Yii2自动加载脚本文件?

My view 我的看法

  <?php

   use yii\helpers\Html;
   use yii\helpers\ArrayHelper;
   use yii\helpers\UrlManager;
   use yii\widgets\ActiveForm;
   use backend\controllers\StandardController;

   use backend\models\standard;


   ?>

 <div class="domain-form">

<?php $form = ActiveForm::begin(); ?>

<?php



    <?= $form->field($model, 'clause')->textarea(['rows' => 6]) ?>

    <?= $form->field($model, 'name')->textarea(['rows' => 6]) ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

<?php ActiveForm::end(); ?>

   </div>

    <script type="text/javascript">
    $("document").ready( function () {
    alert("hi");
    });</script>

I need to get this simple script to show an alert after the page loaded. 我需要让这个简单的脚本在页面加载后显示警告。 I hav'nt called any of the script file here since yii loads automatically(i think) in the layout by calling 我没有在这里调用任何脚本文件,因为yii通过调用自动加载(我认为)在布局中

  AppAsset::register($this);

This results in script files to be registered at the end of the page,after my custom script. 这导致脚本文件在我的自定义脚本之后在页面末尾注册。

How to solve this? 怎么解决这个?

Yii2 injects scripts (jquery and the like) last on your page. Yii2最后在你的页面上注入脚本(jquery等)。 This is intended and desired. 这是有意的。 But it means that the jQuery will load AFTER your script, so when your script runs, jQuery does not yet exist. 但这意味着jQuery将在你的脚本之后加载,所以当你的脚本运行时,jQuery还不存在。

The easiest way for quick testing is to move yii-scripts (jquery and the like) to the head of the page. 快速测试的最简单方法是将yii-scripts(jquery等)移动到页面的头部。 Modify assets\\AppAsset.php and add this: 修改assets\\AppAsset.php并添加:

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

Done! 完成!


But in production you usually want the scripts to load last, and instead you let Yii2 handle your javascript: 但是在生产中你通常希望脚本最后加载,而你让Yii2处理你的javascript:

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

Now Yii will handle this js and place it after anything important (like jQuery). 现在Yii将处理这个js并将它放在任何重要的东西之后(比如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. 然而,您很快就会注意到IDE通常不善于处理这种语言嵌套(PHP中的JavaScript),因此语法突出显示可能会破坏。 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 . 添加脚本的推荐方法是使用AssetBundles Look in assets/AppAssets.php and add your js-file to the $js-array. 查看assets/AppAssets.php并将您的js文件添加到$ js-array。

Whenever you need some assets for a library specific use, register them in the view file like this: 每当您需要某些资源用于库特定用途时,请在视图文件中注册它们,如下所示:

use yii\web\JqueryAsset;
JqueryAsset::register(this);

Alternatively, if you add yii\\web\\JqueryAsset to your $depends attribute inside AppAsset , the asset will be loaded automatically for all your views, given that you registered it in the main layout (by default in Yii2 boilerplate). 或者,如果将yii\\web\\JqueryAsset添加到yii\\web\\JqueryAsset中的$depends属性,则会自动为所有视图加载资源, AppAsset是您已在主布局中注册(默认情况下在Yii2样板中)。

I wanted to use the yii2-date-range from kartik-v in my project, and did this: 我想在我的项目中使用kartik-v的yii2-date-range,并且这样做:

use kartik\daterange\DateRangePicker;
use kartik\daterange\MomentAsset;
use yii\web\JqueryAsset;

JqueryAsset::register(this);
MomentAsset::register(this);
...
echo DateRangePicker::widget([ 
    'model' => $model, 
    ...
]);

Since I switched to twig templating, I ended up with the following code in my view: 自从我切换到twig模板后,我在我的视图中得到了以下代码:

{{ use('kartik/daterange/DateRangePicker') }}
{{ use('kartik/daterange/MomentAsset') }}
{{ use('yii/web/JqueryAsset') }}
{{ register_jquery_asset() }}
{{ register_moment_asset() }}
...
{{ date_range_picker_widget({ 
    'model': model, 
    ...
   }) }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM