2

I have added fullcalendar js/css in vendor/bower folder. I want to add this into just one page.

I read abt AssestBundle on this link - http://www.yiiframework.com/doc-2.0/guide-structure-assets.html

But this add in all the pages.

4

1 回答 1

3

在 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) 中的文件会在每个视图中加载,因为它已在应用程序布局中注册,并且布局应用于所有视图。

于 2016-08-28T13:54:45.013 回答