如何管理Yii2中的资产? [英] How do I manage assets in Yii2?
问题描述
例如,我创建了一个新页面,并且我想使用例如ribs.js,自定义css文件和一些图像集合.我应该在Yii2的哪里声明所有这些东西?我找到了AppAsset.php模块,但这仅用于css/js文件,并且在那里声明了我的css/js文件和路径时,我没有注意到任何更改:
For example, I created a new page, and I'd like to use, for example, backbone.js, custom css file and some collection of images. Where should I declare all this stuff in Yii2? I found the AppAsset.php module, but this is only for css/js files and I haven't noticed any changes when my css/js files and path were declared there:
class AppAsset extends AssetBundle {
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'css/site.css',
'js/jquery.mobile-1.4.2.min.css',
];
public $js = [
'js/jsquery-2.1.0.min.js',
'js/jquery.mobile-1.4.2.min.js',
'js/script.js',
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
我做错了什么?
推荐答案
It took me a while to figure it out, but below is the relevant part of the Yii2 source code
if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) {
list ($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
}
因此,仅当设置了$sourcePath
且未设置$basePath
和$baseUrl
时,Yii2才会发布资产.后者使我绊倒,看起来同样适合您.
So Yii2 will publish assets only if $sourcePath
is set, and $basePath
and $baseUrl
are not set(!). The latter tripped me up, and it looks like the same goes for you.
所以我有这个AppAsset,它已经发布了
So I have this AppAsset, which duly publishes
use yii\web\AssetBundle;
class AppAsset extends AssetBundle
{
public $sourcePath = '@app/assets/app';
public $css = [
'css/openbook.css',
'fontello/css/fontello.css',
'fontello/css/animation.css'
];
public $js = [
'js/plug.openbook.js',
'js/plug.interpret.js',
'js/plug.drop.message.js'
];
public $depends = [
// 'yii\web\YiiAsset',
// 'yii\bootstrap\BootstrapAsset',
];
}
我当然在主布局中
use frontend\assets\AppAsset;
...
AppAsset::register($this);
这篇关于如何管理Yii2中的资产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!