yii2资产包 - 单个文件位置

spa*_*pis 7 yii2 assetbundle

我在Yii2框架上做一个Web应用程序.定义(扩展)新的AssetBundle

class NeonAsset extends AssetBundle {
   public $sourcePath = '@app/themes/neon/';

   public $css = [
    'css/font-icons/entypo/css/entypo.css',
    '...',
    '...'        
   ];

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

渲染时,CSS文件正在<head>标记中发布,JS文件在<body>标记的底部发布.没关系.
但是我希望be_in_head_tag.js<head>标签中发布一个文件.当我使用$jsOptions它时,将所有JS文件移动到<head>标记中.
是否可以只为一个文件制作选项?

iam*_*eek 9

其中一个变体将该文件转换为单独的资产类:

class HeadPublishAsset extends AssetBundle {
    public $sourcePath = '@app/themes/neon/';
    public $js = ['js/be_in_head_tag.js'];
    public $jsOptions = ['position' => \yii\web\View::POS_HEAD];
}
Run Code Online (Sandbox Code Playgroud)

并为您的基类添加依赖项,如:

class NeonAsset extends AssetBundle {
    ...
    public $depends = ['app\assets\HeadPublishAsset'];
    ...
}
Run Code Online (Sandbox Code Playgroud)