Yii2 Uncaught ReferenceError: $ is not defined

Yii2 Uncaught ReferenceError: $ is not defined

我的资产:

class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/site.css',
    ];
    public $js = [
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
        'yii\web\JqueryAsset'
    ];
}

MyMain.php 已注册此资产:

AppAsset::register($this);

在我的 test.php 中使用 jquery

<div class="container">
<div class="row">
    <div class='col-sm-6'>
        <div class="form-group">
            <div class='input-group date' id='datetimepicker1'>
                <input type='text' class="form-control" />
                <span class="input-group-addon">
                    <span class="glyphicon glyphicon-calendar"></span>
                </span>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        $(function () {
            $('#datetimepicker1').datetimepicker();
        });
    </script>
</div>
</div>

有这个错误:

Uncaught ReferenceError: $ is not defined

希望大家能帮帮我。谢谢!

这是 yii2 的默认 AppAset.php(此示例用于后端)

  <?php
  /**
   * @link http://www.yiiframework.com/
   * @copyright Copyright (c) 2008 Yii Software LLC
   * @license http://www.yiiframework.com/license/
   */

  namespace backend\assets;

  use yii\web\AssetBundle;

  /**
   * @author Qiang Xue <qiang.xue@gmail.com>
   * @since 2.0
   */
  class AppAsset extends AssetBundle
  {
      public $basePath = '@webroot';
      public $baseUrl = '@web';
      public $css = [
          'css/site.css',
      ];
      public $js = [
      ];
      public $depends = [
          'yii\web\YiiAsset',
          'yii\bootstrap\BootstrapAsset',
      ];
  }

如您所见,没有对 jquery 的引用,因为这通常由 yii2 Asset bundle 管理

尝试将此内容用于 Appset.php(最终使用您需要的名称空间),同时引用其他 jquery 资产并使用我们的寄存器 .. 这 mnormally 在高级模板中工作

您应该通过资产或方法 registerJs 使用 javascript 脚本,这里是示例

<?php $this->registerJs('$(function () { $(\'#datetimepicker1\').datetimepicker();});', View::POS_END); ?>