yii2-按需加载并管理CSS样式/JS脚本

(注:如下为Yii2.0高级;注意代码中php标签本身补上)php

1、资源包定义

Yii2对于CSS/JS 管理,使用AssetBundle资源包类。
建立以下:
backend/assets/AppAsset.phpcss

namespace backend\assets;  
  
use yii\web\AssetBundle;  
  
/** 
 * @author chan <maclechan@qq.com> 
 * @since 2.0 
 */  
class AppAsset extends AssetBundle  
{  
    public $basePath = '@webroot';  
    public $baseUrl = '@web';  
    //全局CSS  
    public $css = [  
        'css/animate.css',  
        'css/style.min.css',  
    ];  
//全局JS  
public $js = [  
    'js/jquery-2.1.1.js'  
]; 
    //依赖关系  
    public $depends = [  
        'yii\web\YiiAsset',  
        'yii\bootstrap\BootstrapAsset',  
    ];  
  
     //定义按需加载JS方法,注意加载顺序在最后  
    public static function addScript($view, $jsfile) {  
        $view->registerJsFile($jsfile, [AppAsset::className(), 'depends' => 'backend\assets\AppAsset']);  
    }  
      
   //定义按需加载css方法,注意加载顺序在最后  
    public static function addCss($view, $cssfile) {  
        $view->registerCssFile($cssfile, [AppAsset::className(), 'depends' => 'backend\assets\AppAsset']);  
    }  
 }

2、视图使用:

1. 视图(或布局)使用全局CSS/JS

use yii\helpers\Html;  
use backend\assets\AppAsset;  
use backend\widgets\Alert;  
  
/* @var $this \yii\web\View */  
/* @var $content string */  
  
AppAsset::register($this);

查看源文件,看清CSS和JS的加载顺序
图片描述jquery

能够看出以此顺序为:依赖关系 -> 自定义全局CSS/JS
若是,某个视图须要单独引入一个CSS/JS,而且,在页面中还要写些CSS/JS,该如何作?web

2. 在页面中单独写样式

$cssString = ".gray-bg{color:red;}";  
$this->registerCss($cssString);

3. 在页面中单独写JS(使用数据块)

<div id="mybutton">点我弹出OK</div>  
  
<?php $this->beginBlock('test') ?>  
    $(function($) {  
      $('#mybutton').click(function() {  
        alert('OK');  
      });  
    });  
<?php $this->endBlock() ?>  
<?php $this->registerJs($this->blocks['test'], \yii\web\View::POS_END); ?>

或者:bootstrap

<?php
$this->registerJs(
   '$("document").ready(function(){ 
        $("#login-form").validate({
            errorElement : "small", 
            errorClass : "error",
            rules: {
                     "AgNav[nav_cn]": {
                         required: true,
                     },
            },
            messages:{
                   "AgNav[nav_cn]" : {  
                        required : "此字段不能为空.",
                    },
            }
        });
    });'
);
?>

4.视图中引入CSS/JS文件

而后再说下在视图中如何引入一个CSS/JS文件(不是定义在全局里的CSS或JS)
分别有两种方法:yii

  • 方法1
    在资源包管理器里面定义一个方法,而后在视图中注册便可(推荐使用这种)布局

如上面代码己定义:ui

//定义按需加载JS方法,注意加载顺序在最后  
public static function addScript($view, $jsfile) {  
    $view->registerJsFile($jsfile, [AppAsset::className(), 'depends' => 'backend\assets\AppAsset']);  
}

视图中使用以下this

AppAsset::register($this);  
//只在该视图中使用非全局的jui   
AppAsset::addScript($this,'@web/js/jquery-ui.custom.min.js');  
//AppAsset::addCss($this,'@web/css/font-awesome/css/font-awesome.min.css');

查看下源码,特别的注意下,加载的顺序,是咱们想要的结果
图片描述spa

此外注意:在上面的addScript方法中,若是没有 ’depends‘=>’xxx‘ ,此处加载的顺序将会颠倒。

  • 方法2
    不须要在资源包管理器中定义方法,只要在视图页面直接引入便可

AppAsset::register($this);  
//css定义同样  
$this->registerCssFile('@web/css/font-awesome.min.css',['depends'=>['backend\assets\AppAsset']]);  
//$this->registerCssFile('@web/css/index-css.css');
  
 $this->registerJsFile('@web/js/jquery-ui.custom.min.js',['depends'=>['backend\assets\AppAsset']]);
//$this->registerJsFile('@web/js/jquery-2.1.1.js'); 
 
//以下position是让定义CSS/JS出现的位置
//$this->registerJsFile('@web/js/jquery-ui.custom.min.js',['depends'=>['backend\assets\AppAsset'],'position'=>$this::POS_HEAD]);

图片描述

相关文章
相关标签/搜索