0

我已经在 yii2-advanced 中安装了yii2-bootstrap4扩展,并添加了一个自定义的 css 文件 ( custom.css) 使用 Sass 重新编译 Bootstrap 源。

然后我添加custom.cssfrontend/web/css修改frontend/assets/AppAsset.php如下:

class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/custom.css',
        'css/site.css',
    ];
    public $js = [
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap4\BootstrapAsset',
    ];
}

我得到了我想要的结果,但我注意到<head>...</head>我的页面包含以下内容:

<link href="/yii2-advanced/frontend/web/assets/1758a11a/css/bootstrap.css" rel="stylesheet">
<link href="/yii2-advanced/frontend/web/css/custom.css" rel="stylesheet">
<link href="/yii2-advanced/frontend/web/css/site.css" rel="stylesheet"></head>

和:

  • [...]/css/bootstrap.css包含原始的 Bootstrap 4 css
  • 如果我从 DOM 中删除第一个条目(通过 Devtools),则网页不会受到影响。

问题

  1. 这是在 Yii2 中替换 Bootstrap 4 css 文件的正确方法吗?
  2. 有没有办法防止加载[...]/css/bootstrap.css
4

1 回答 1

1

改编this other question的答案之一(由@Sfili_81建议),我解决frontend/assets/AppAsset.php了如下修改:

class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/custom.css',
        'css/site.css',
    ];
    public $js = [
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap4\BootstrapAsset',
    ];

    public function init(){
        parent::init();
        // resetting BootstrapAsset to not load own css files
        \Yii::$app->assetManager->bundles['yii\\bootstrap4\\BootstrapAsset'] = [
            'css' => [],
            //'js' => []
        ];
    }
}

现在该<head>部分中包含的 css 文件只有这些:

<link href="/yii2-advanced/frontend/web/css/custom.css" rel="stylesheet">
<link href="/yii2-advanced/frontend/web/css/site.css" rel="stylesheet">
于 2020-02-21T13:53:23.287 回答