具體操作如下:
(學習視頻分享:編程視頻)
1、在 assets/AppAsset 里定義方法
<?php /** * @link http://www.YIIframework.com/ * @copyright Copyright (c) 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ */ namespace appassets; use yiiwebAssetBundle; /** * @author Qiang Xue <qiang.xue@gmail.com> ?*?@since?2.0 ?*/ class?AppAsset?extends?AssetBundle { ????public?$basePath?=?'@webroot'; ????public?$baseUrl?=?'@web'; ? ????//?全局 ????public?$css?=?[ ???????? ????]; ? ????//?全局 ????public?$js?=?[ ???????? ????]; ? ????public?$depends?=?[ ????????//?'yiiwebYiiAsset', ????????//?'yiibootstrapBootstrapAsset',??//?注釋掉禁用bootstrap ????]; ? ????//?這是設置所有js放置的位置? ????public?$jsOptions?=?[?? ????????'position'?=>?yiiwebView::POS_HEAD,???? ????];? ? ????//定義按需加載JS方法 ????public?static?function?addJs($view,?$jsfile)?{? ????????$view->registerJsFile( ????????????$jsfile,? ????????????[ ????????????????AppAsset::className(),? ????????????????"depends"?=>?"appassetsAppAsset" ????????????] ????????);? ????}? ? ????//定義按需加載css方法 ????public?static?function?addCss($view,?$cssfile)?{? ????????$view->registerCssFile( ????????????$cssfile,? ????????????[ ????????????????AppAsset::className(),? ????????????????"depends"?=>?"appassetsAppAsset" ????????????] ????????);? ????}? ? }
2、在view里調用
<?php /* @var $this yiiwebView */ /* @var $content string */ use yiihelpersHtml; use yiibootstrapNav; use yiibootstrapNavBar; use yiiwidgetsBreadcrumbs; use appassetsAppAsset; // 注冊全局加載 AppAsset::register($this); // 按需加載css AppAsset::addCss($this, Yii::$app->request->baseUrl."/css/site.css"); //?按需加載js AppAsset::addJs($this,?Yii::$app->request->baseUrl."/js/respond.min.js"); ? ?> <?php $this->beginPage()??> nbsp;html> language??>"> ????<meta>charset??>"> ????<meta> ????<meta> ????=?Html::csrfMetaTags()??> ????<title>=?Html::encode($this->title)??></title> ????<?php $this->head()??> <?php $this->beginBody()??> ? ???????? ????=?$content??> ? <?php $this->endBody()??> <?php $this->endPage()??>
相關推薦:編程視頻
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END