Yii2 How to prevent autoloading of assetsbundle of

2019-08-09 11:30发布

问题:

I am using \yii\jui\AutoComplete

It automatically loads the JuiAssets. Is there any way to prevent loading files of JuiAssets? I want to use my own version of files. In the previous version of Yii Framework, we had option to prevent loading the associated css and js files of any widget.

Is there any way to change the AssetBundle for any jui Widget, without editing its core files?

回答1:

Yes, you can easily customize any AssetBundle to fit your needs:

1) Through application config:

return [
    'components' => [
        'assetManager' => [
            'bundles' => [
                'yii\jui\JuiAsset' => [
                    'sourcePath' => null, // do not publish the bundle
                    'js' => [
                        // replace published js file here
                    ],
                ],
            ],
        ],
    ],
];

2) You can do the same at runtime through assetManager component:

Yii::$app->assetManager->bundles['yii\jui\JuiAsset'] = [
    'sourcePath' => null,
    'js' => [...],
];

Official documentation:

  • Customizing Asset Bundles
  • assetManager component

Note that logic for registering jQuery UI components was changed in this commit. So there is no separate asset bundle for Autocomplete, etc. They all use yii\jui\JuiAsset.