How can I add conditions to yii2 depends in AppAsset class?(如何在 AppAsset 类中向 yii2 依赖项添加条件?)
问题描述
在 Yii2 的 AppAsset 类中有一些依赖:
There are some depends in AppAsset class in Yii2:
public $depends = [
'yiiwebYiiAsset',
'yiiootstrapBootstrapAsset'
];
有没有办法像我用 css 和 js 那样为此依赖添加条件?
Is there any way to add conditions for this depends like I do it with css and js?
public $jsOptions = ['condition' => 'lt IE 7'];
或者,您知道另一种向 bootstrap 和 yii JS 和 CSS 文件添加条件的方法吗?
谢谢
Or, may be, you know another way to add conditions to bootstrap and yii JS and CSS files?
Thanks
UPD:我已添加到 config/web.php:
UPD: I've added to config/web.php:
'components' => [
'assetManager' => [
'bundles' => [
'yiiwebYiiAsset' => [
'jsOptions' => ['condition' => 'lt IE 7'],
],
'yiiootstrapBootstrapAsset' => [
'jsOptions' => ['condition' => 'lt IE 7'],
],
],
],
但是,我有这个(正如@arogachev 推荐的那样):
But, I've got this (as @arogachev recommends):
<script src="L2Fzc2V0cy84ZmQyNDRjNi9qcXVlcnkuanM="></script>
<!--[if lt IE 7]>
<script src="L2Fzc2V0cy9iZDQ4YzQ2NS95aWkuanM="></script>
<![endif]-->
<script src="L2Fzc2V0cy9iZDQ4YzQ2NS95aWkuZ3JpZFZpZXcuanM="></script>
<script src="L2Fzc2V0cy9kYjljYjlhYS9qcy9ib290c3RyYXAuanM="></script>
UPD2:答案
'yiiwebYiiAsset' => [
'cssOptions' => ['condition' => 'gt IE 7]>'],
'jsOptions' => ['condition' => 'gt IE 7]>'],
],
'yiiootstrapBootstrapAsset' => [
'cssOptions' => ['condition' => 'gt IE 7]>'],
],
'yiiootstrapBootstrapPluginAsset' => [
'jsOptions' => ['condition' => 'gt IE 7]>'],
],
'yiiwebJqueryAsset' => [
'jsOptions' => ['condition' => 'gt IE 7]>'],
'cssOptions' => ['condition' => 'gt IE 7]>'],
],
推荐答案
您可以像这样通过应用程序配置自定义供应商捆绑包:
You can customize vendor bundles through application config like that:
return [
// ...
'components' => [
'assetManager' => [
'bundles' => [
'yiiwebYiiAsset' => [
'jsOptions' => ['condition' => 'lt IE 7'],
],
'yiiootstrapBootstrapAsset' => [
'jsOptions' => ['condition' => 'lt IE 7'],
],
],
],
],
];
或者在运行时通过assetManager
:
use Yii;
...
Yii::$app->assetManager->bundles['yiiwebYiiAsset']->jsOptions = ['condition' => 'lt IE 7'];
Yii::$app->assetManager->bundles['yiiootstrapBootstrapAsset']->jsOptions = ['condition' => 'lt IE 7'];
官方文档:
- 自定义资产包
这篇关于如何在 AppAsset 类中向 yii2 依赖项添加条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 AppAsset 类中向 yii2 依赖项添加条件?


- PHP foreach() 与数组中的数组? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01