参考错误:$ 未定义 yii2 [英] ReferenceError: $ is not defined yii2

查看:42
本文介绍了参考错误:$ 未定义 yii2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的视图中添加 javascript 会导致 ReferenceError: $ is not defined.我认为问题是由于 Yii2 最后在我的页面上注入了脚本.如何解决这个问题?

或者如何防止 Yii2 自动加载脚本文件?

我的观点

 <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']) ?>

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

<script type="text/javascript">$("document").ready( function () {警报(嗨");});

我需要使用这个简单的脚本来在页面加载后显示警报.我没有在这里调用任何脚本文件,因为 yii 通过调用在布局中自动加载(我认为)

 AppAsset::register($this);

这会导致在我的自定义脚本之后的页面末尾注册脚本文件.

如何解决这个问题?

解决方案

Yii2 在页面的最后注入脚本(jquery 等).这是有意和希望的.但这意味着 jQuery 将在您的脚本之后加载,因此当您的脚本运行时,jQuery 尚不存在.

快速测试最简单的方法是将 yii-scripts(jquery 等)移动到页面的头部.修改 assets\AppAsset.php 并添加:

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

完成!

<小时>

但在生产环境中,您通常希望脚本最后加载,而是让 Yii2 处理您的 javascript:

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

现在 Yii 会处理这个 js 并将它放在任何重要的东西之后(比如 jQuery).

但是您很快就会注意到 IDE 通常不擅长处理这种语言嵌套(PHP 中的 JavaScript),因此语法突出显示可能会中断.解决它的一种方法是在单独的文件中注册您的脚本:

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

如果您想要更多地控制加载脚本的顺序,您可以添加依赖项作为第二个参数,并添加其他选项作为第三个:

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

如果您出于某种原因绝对希望脚本内联呈现,您可以这样做:

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

<小时>

推荐的添加脚本的方法是使用 AssetBundles.查看 assets/AppAssets.php 并将您的 js 文件添加到 $js-array 中.

Adding a javascript inside my view results in the ReferenceError: $ is not defined. I assume that the problem is due to Yii2 injects scripts last on my page. How to fix this?

Or how will I prevent Yii2 from autoloading the script files?

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

  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. 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.

The easiest way for quick testing is to move yii-scripts (jquery and the like) to the head of the page. Modify assets\AppAsset.php and add this:

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:

$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.

这篇关于参考错误:$ 未定义 yii2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆