yii2 布局文件 CSS JS layout asset使用

新建资源文件(JS CSS):

assets/文件夹名称/资源文件名称

        <?php
        	//assets/文件夹名称
            namespace app\assets\gm;
            use app\assets\AppAsset;
            //继承AppAsset
            class BsAsset extends AppAsset
            {
                public $css=[
                //CSS文件路径
                ];
            
                public $js=[
                // JS文件路径
                    'lab/jquery/jquery-2.1.4.min.js',
                    'js/vue2.6.11.js',
                ];
            
                public $depends=[
                ];
            }

使用资源文件

views/layouts/文件夹名称/文件名称

        <?php
            use app\assets\gm\BsAsset;
            BsAsset::register($this);
        ?>
        <?php $this->beginPage() ?>
            <!DOCTYPE html>
            <html lang="<?= Yii::$app->language ?>">
            <head>
                <meta charset="<?= Yii::$app->charset ?>">
            <!--<meta name="viewport" content="width=device-width, initial-scale=1">-->
            <!--不缓存浏览器数据-->
            <!--<meta http-equiv="pragma" content="no-cache" />-->
            <!-- <title>管理后台</title>-->
                <?php $this->head() ?>
            </head>
            <?php $this->beginBody() ?>
            <?=$content?>
            <?php $this->endBody() ?>
            </html>
        <?php $this->endPage() ?>

布局文件使用

//方案1:控制器内成员变量
public $layout = false; //不使用布局
public $layout = "main"; //设置使用的布局文件

//方案2:控制器成员方法内
$this->layout = false; //不使用布局
$this->layout = "main"; //设置使用的布局文件

//方案3:视图中选择布局
$this->context->layout = false; //不使用布局
$this->context->layout = 'main'; //设置使用的布局文件
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章