如何为Ass​​etBundle中的CSS添加"type ='text/css'" - Yii2

Nan*_*kar 3 php yii2

我正在使用My AppAsset这样的方式.

AppAsset.php

<?php
namespace app\assets;
use yii\web\AssetBundle;

class AppAsset extends AssetBundle
{
  public $basePath = '@webroot';
  public $baseUrl = '@web';
  public $css = [
    'css/style.css',
  ];
  public $js = [
    'js/myscript.js',

  ];

  public $depends = [
    'yii\web\YiiAsset',
    'yii\bootstrap\BootstrapAsset',
    'yii\web\JqueryAsset',
  ];
}
Run Code Online (Sandbox Code Playgroud)

但是,当我按Ctrl+ U查看源代码时,

我得到<link href="/css/style.css" rel="stylesheet">.但是,没有type='text/css'.

如何为type='text/css'我的所有CSS 添加?

Nan*_*kar 5

@Moped回答:

怎么样$cssOptions = array('type'=>'text/css');..从未使用过Yii,但它可以在他们的Docs上找到

我们可以使用$ cssOptions来为CSS添加属性.

public $css = [
    'css/style.css',
];

public $cssOptions = ['type'=>'text/css'];  

public $js = [
   'js/myscript.js',    
];
Run Code Online (Sandbox Code Playgroud)

并且,@ Quentin太正确了:

关键是type属性是可选的,如果省略它并指定,rel=stylesheet那么浏览器将默认为text/css.添加类型属性不会有任何区别.

因为添加后$cssOptions,虽然链接看起来像<link type="text/css" href="/css/style.css" rel="stylesheet">.但是,它对CSS没有任何影响.