阿刚 2018-08-09 16:27:36 2193次浏览 2条回复 0 0 0

yii2 高级版

common\config\main.php 这样配置

'assetManager' => [
    'linkAssets' => false,
    'bundles' => [
       'yii\web\JqueryAsset' => [
            'js' => [
               'jquery.js'
            ],
        ],
    ],
],

frontend\config\main.php 中这样配置

'bundles' => [
    'yii\web\JqueryAsset' => [
        'js' => [],  // 去除 jquery.js
        'sourcePath' => null,  // 防止在 frontend/web/asset 下生产文件
    ],
]

结果并不能去除jquery

  • 回复于 2018-08-09 16:44 举报

    找到一个解决方案覆盖这样写

    'bundles' => [
        'yii\web\JqueryAsset' => [
            'js' => [
                'a' => null
    
            ],  // 去除 jquery.js
            'sourcePath' => null,  // 防止在 frontend/web/asset 下生产文件
        ],
    ]
    
  • 回复于 2018-08-09 19:01 举报

    不是 bug. frontend 配置能不能覆盖 common 配置由 ArrayHelper::merge() 的实现方式决定,与 asset manager 无关。

    For integer-keyed elements, the elements from the latter array will be appended to the former array. You can use yii\helpers\UnsetArrayValue object to unset value from previous array or yii\helpers\ReplaceArrayValue to force replace former value instead of recursive merging.

    https://www.yiiframework.com/doc/guide/2.0/en/helper-array#merging-arrays

    对照上面的说明,不难理解为什么你的配置合并后并未去除 jQuery: 因为 common 和 frontend 中 js option 的值都是 integer-keyed elements (分别是 ['jquery.js'][]), 所以 Yii 将 [] 直接 append to ['jquery.js'], 最终的结果还是 ['jquery.js'], 即需要 jQuery.

    如果想要达到强制覆盖的效果,文档中讲得也很清楚:使用 yii\helpers\ReplaceArrayValue 即可,例如,你的 backend 配置数组可以这么写:

    'bundles' => [
        'yii\web\JqueryAsset' => [
            'js' => new \yii\helpers\ReplaceArrayValue([]),
        ],
    ],
    

    更省事的办法是像你说的解决方案那样,直接将后者的值显性赋值为 null, 原理就是让两个值得类型不同,类型一旦不同,后者自然就直接覆盖前者了。

    1 条回复
    回复于 2018-08-23 08:14 回复

    你的回答最专业!

    觉得很赞
您需要登录后才可以回复。登录 | 立即注册