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.

时间: 2024-08-05 12:33:51

BUG YII2.0 $ is not defined的相关文章

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

BUG描述:登录直接显示 原因:服务器设置端口权限,或者DNS毛病 解决方案:只能去服务器端设置,配置端口 DNS: 修改dns 114.114.114.114 或者 8.8.8.8

在windows7上的通过composer安装yii2.0

在 Windows 中,你首先需要下载并运行 Composer-Setup.exe 1.下载composer.exe 下载网址:https://getcomposer.org/download/ 2.开启PHP的openssl支持 修改PHP安装目录下的php.ini文件,比如:D:\appSer\php5,配置文件php.ini, 去掉注释 3.安装Composer 一直点击next即可,他会自动识别出你的php.exe.安装完毕会提示你重新打开CMD命令行使用. 在命令行中输入compose

Linux下安装Yii2.0

1.若还没有安装 Composer ,则在终端里输入以下命令,先安装 Composer(等待时间有点长,请耐心等待,下同): 1 2 #若复制到终端里运行不了,请手工输入(下同) curl -sS https://getcomposer.org/installer | php 2.运行如下命令来安装 Composer Asset插件: 1 php composer.phar global require "fxp/composer-asset-plugin:1.0.0-beta3" 可

在beforeAction里redirect无效,Yii2.0.8

我是在官方GitHub上得到回答,试了一下,确实解决问题了.之前的问题描述: 之前是2.0.3,然后用composer直接升级到2.0.8,就不正常了,以为是我代码的问题,于是再次尝试 用composer下载了yii-advance-2.0.8 ,在beforeAction里跳转还是不行,于是上论坛问问 放在普通的action里就没有问题,但是放在beforeAction里就是不跳转,相当于返回的是true,直接略过. 不跳转代码: return $this->redirect('http://

composer安装yii2.0

没有composer: curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer    //加载全局环境 安装yii2: composer global require "fxp/composer-asset-plugin:~1.1.1" composer create-project yiisoft/yii2-app-basic basic 2.0.6 链接:ht

Yii1.1应用升级到Yii2.0的一些注意点

1. 应用程序实例直接使用全局命名变量来訪问:$app,而无须调用app() 2. Yii2在视图层的一个显著改变是引入了视图类,这样在MVC模式实现上更为完整. 对应的,相关展现层附属类就由新的视图类来接手管理,比方theme: Yii::app()->theme->baseUrl 应该更新为 Yii::$app->view->theme->baseUrl,或者$this->theme->baseUrl. 3. Yii2引入了资源包的概念,资源引用的方式变化较大

高可用高性能yii2.0

Yii2 是什么? Yii2 是一个高性能,基于组件的 PHP 框架,用于快速开发现代 Web 应用程序. 名字 Yii2 (读作 易)在中文里有“极致简单与不断演变”两重含义,也可看作 Yes It Is! 的缩写. Yii2 最适合做什么? Yii2 是一个通用的 Web 编程框架,即可以用于开发各种用 PHP 构建的 Web 应用. 因为基于组件的框架结构和设计精巧的缓存支持,它特别适合开发大型应用,如门户网站.社区.内容管理系统 (CMS).电子商务项目和 RESTful Web 服务等

Windows下安装yii2.0

先将官网下载的压缩包解压,将advanced文件夹在网站根目录中 2.右键"我的电脑"-> 属性 ->高级系统设置,将php.exe所在目录(c:\xampp\php)以及c:\xampp\php\ext目录的路径加入到path中 3.运行advanced下init.bat程序,执行init或在安装目录中运行init.bat,选择 0 开发模式进行安装,选yes,还有提示就选all 4.访问http://127.0.0.1/advanced/frontend/web/ind

yii2.0 行为

行为(Behavior) 使用行为(behavior)可以在不修改现有类的情况下,对类的功能进行扩充. 通过将行为绑定到一个类,可以使类具有行为本身所定义的属性和方法,就好像类本来就有这些属性和方法一样. 而且不需要写一个新的类去继承或包含现有类. Yii中的行为,其实是 yii\base\Behavior 类的实例, 只要将一个Behavior实例绑定到任意的yii\base\Component 实例上, 这个Component就可以拥有该Behavior所定义的属性和方法了.而如果将行为与事