我在vendor / bower文件夹中添加了fullcalendar js / css。我想将此添加到仅一页中。
我在此链接上阅读了abt AssestBundle- http: //www.yiiframework.com/doc-2.0/guide-structure-assets.html
但这会添加到所有页面中。
在Yii 2框架中,建议使用资产捆绑包与js / css一起工作。它不仅限于添加到所有页面。您只能在特定视图中使用它。
JsTree
插件的资产捆绑示例:
<?php
namespace backend\assets;
use yii\web\AssetBundle;
class JsTreeAsset extends AssetBundle
{
public $sourcePath = '@bower_components/jstree/dist';
public $js = [
'jstree.min.js',
];
public $css = [
'themes/default/style.min.css',
];
public $depends = [
'yii\web\JqueryAsset',
];
}
在此示例中,使用了@bower_components
别名,为了使其正常工作,您还需要在应用程序引导程序文件中注册它(在高级应用程序模板中,该文件为common/config/bootstrap.php
):
Yii::setAlias('bower_components', dirname(dirname(__DIR__)) . '/bower_components');
然后,在需要使用它的视图中,调用register()
此资产捆绑的方法并传递当前视图:
use backend\assets\JsTreeAsset;
...
JsTreeAsset::register($this);
AppAsset
应用程序模板中包含的默认资产包()中的文件会在每个视图中加载,因为它已在应用程序布局中注册,并且布局已应用于所有视图。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句