Paramore 2016-04-20 12:18:07 6637次浏览 5条回复 0 0 0

大家好! 我做了一个后台,在 view 中有这两行来引入 css 和 Js

use frontend\assets\TestAsset;
TestAsset::register( $this );

由于YII默认是有layout (view/layout/main.php), 那我就想禁用这个 layout,只使用我写的 css 和 js 就好了。 于是, 我在 controller 中写了 public $layout = false; 然后,layout 确实是没有了,但是我在 view 中 asset 引入的资源没有了。。

这是为什么呢? 感谢大家!

  • 回复于 2016-04-20 15:18 举报

    $layout = false;意思是不引用模板 就不能使用该模板引用的样式和js 代表该页面需要重新引用所有css和js

    觉得很赞
  • 回复于 2016-04-22 08:37 举报

    取消layout,需在自己在view中实现 head 部分, asset 需要先实现head部分才能引入。 在layout 中你可以看到 <?php $this->head() ?>

    觉得很赞
  • 回复于 2016-04-24 19:18 举报

    感谢两位! 我明白了!

  • 回复于 2016-04-28 17:46 举报

    那具体应该怎么写呢

    觉得很赞
  • 回复于 2016-04-28 21:12 举报

    这是我修改后的 views/layouts/main.php 文件 你参考下吧

    <?php
    
    /* @var $this \yii\web\View */
    /* @var $content string */
    
    use yii\helpers\Html;
    use yii\bootstrap\Nav;
    use yii\bootstrap\NavBar;
    use yii\widgets\Breadcrumbs;
    use app\assets\AppAsset;
    
    ?>
    <?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">
        <?= Html::csrfMetaTags() ?>
        <title><?= Html::encode($this->title) ?></title>
        <?php $this->head() ?>
    </head>
    <body>
    <?php $this->beginBody() ?>
    
    <div class="wrap">
            <?= $content ?>
        </div>
    </div>
    
    <?php $this->endBody() ?>
    </body>
    </html>
    <?php $this->endPage() ?>
    
    觉得很赞
您需要登录后才可以回复。登录 | 立即注册