AppAsset.php
<?php
namespace app\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/main.css'
];
public $js = [
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
//定义按需加载JS方法,注意加载顺序在最后
public static function addScript($view, $jsfile) {
$view->registerJsFile($jsfile, [AppAsset::className(), 'depends' => 'api\assets\AppAsset']);
}
//定义按需加载css方法,注意加载顺序在最后
public static function addCss($view, $cssfile) {
$view->registerCssFile($cssfile, [AppAsset::className(), 'depends' => 'api\assets\AppAsset']);
}
}
view文件:index.php
<?php
/* @var $this yii\web\View */
use yii\bootstrap\Collapse;
use app\assets\AppAsset;
AppAsset::register($this);
AppAsset::addCss($this,'@web/css/home/index.css');
//$this->registerCssFile('@web/css/home/index.css');
$this->title = 'CodeNote';
?>
...
运行后出现错误
ReflectionException
Class apiassetsAppAsset does not exist
删除AppAsset::addCss($this,'@web/css/home/index.css'); 就不会错,为什么?
你AppAsset.php中自定义加载js和css的方法中'depends'的值写成了
api\assets\AppAsset
,应该是app\assets\AppAsset
。另外,建议提问时直接把浏览器的错误提示复制粘贴过来就行,这样可以保留原来的格式(颜色等)高度还原出错场景:),而且还省得手敲了。附注:segmentfault中的编辑器中如果要打反斜杠需要连续敲两个才会出来,这是需要转义么┐(─__─)┌