yii2单个视图加载jss,css

时间:2022-10-28 20:02:31

1,定义资源:首先在AppAsset.php里面定义2个方法,

//按需加载css
public static function addCss($view, $cssfile) {
$view->registerCssFile($cssfile, [AppAsset::className(), 'depends' => 'backend\assets\AppAsset']);
}

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

 

2,使用:注册资源

AppAsset::register($this);
AppAsset::addCss($this,'@web/css/diy.css');